Subversion Repositories wimsdev

Rev

Rev 10 | Rev 3843 | 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 freopen(inf,"r",stdin);
  257.         if(!outf) {
  258.             dup2(pipe_out[1],1); close(pipe_out[0]);
  259.         }
  260.         else freopen(outf,"w",stdout);
  261.         if(errf) freopen(errf,"w",stderr);
  262.         snprintf(abuf,sizeof(abuf),"%s",find_word_start(args));
  263.         if(stat("../chroot/tmp/sessions/.chroot",&st)==0 || must_chroot) {
  264.             arg[0]=ch_root; i=1;
  265.         }
  266.         else {
  267.             i=0;
  268.             setreuid(getuid(),getuid());setregid(getgid(),getgid());
  269.         }
  270.         arg[i++]=cmdf;
  271.         for(p=abuf; *p && i<1000; i++, p=find_word_start(p2)) {
  272.             arg[i]=p; p2=find_word_end(p); if(*p2) *p2++=0;
  273.         }
  274.         arg[i]=NULL;
  275.         if(strchr(arg[0],'/')) execv(arg[0],arg);
  276.         else execvp(arg[0],arg);
  277.         fprintf(stderr,"%s not_INStalled",progname);
  278.         exit(127);
  279.     }
  280.     else {      /* parent */
  281.         addpid(pid); mxpid=pid;
  282.         if(outf) {
  283.             int status;
  284.             close(pipe_in[1]);
  285.             if(waitpid(pid,&status,0)==pid) {
  286.                 rmpid(pid); mxpid=-1;
  287.             }
  288.         }
  289.     }
  290.     return 0;
  291. }
  292.  
  293.         /* verify illegal strings in parms. */
  294. void find_illegal(char *p)
  295. {
  296.     char *pp, *pe;
  297.     int i, l;
  298.     for(pp=p;*pp;pp++) {
  299.         if((*pp<' ' && *pp!='\n' && *pp!='      ') || *pp>=127) *pp=' ';
  300.     }
  301.     for(i=0;i<illpart_no;i++) {
  302.         pe=illpart[i]; l=strlen(pe);
  303.         for(pp=strstr(p,pe); pp; pp=strstr(pp+1,pe)) {
  304.             if(!isupper(pe[0]) || !islower(*(pp+l))) {
  305.                 if(pp[1]!='j' && pp[1]!='J') pp[1]='J';
  306.                 else pp[1]='Z';
  307.             }
  308.         }
  309.     }
  310.     for(i=0;i<illegal_no;i++) {
  311.         pe=illegal[i]; l=strlen(pe);
  312.         for(pp=strstr(p,pe); pp; pp=strstr(pp+1,pe)) {
  313.             if((pp==p || !isalnum(*(pp-1))) && !isalnum(*(pp+l))) {
  314.                 if(pp[1]!='j' && pp[1]!='J') pp[1]='J';
  315.                 else pp[1]='Z';
  316.             }
  317.         }
  318.     }
  319. }
  320.  
  321.         /* strip trailing zeros */
  322. void strip_zeros(char *p)
  323. {
  324.     char *pp, *p2, *numend, *ee;
  325.     int i;
  326.     for(pp=p;*pp!=0;pp++) {
  327.         if(!isdigit(*pp)) continue;
  328.         i=0;
  329.         for(numend=pp;isdigit(*numend) || *numend=='.';numend++)
  330.           if(*numend=='.') i=1;
  331.         if(i==0) {
  332.             pp=numend-1;continue;
  333.         }
  334.         for(p2=numend;p2>pp && *(p2-1)=='0';p2--);
  335.         for(ee=numend;isspace(*ee);ee++);
  336.         if(*(pp+1)=='.' && (*ee=='E' || *ee=='e')
  337.            && *(ee+1)=='-') {
  338.             int k=0;
  339.             char *pt=ee+2;
  340.             while(isdigit(*pt)) {
  341.                 k*=10;k+=*pt-'0';pt++;
  342.             }
  343.             if(precision>8 && (k>precision*2 || (k>precision && *pp=='0'))) {
  344.                
  345.                 sprintf(pp,"0.0%s",pt);
  346.                
  347.                 pp+=strlen("0.0")-1;
  348.                 continue;
  349.             }
  350.         }
  351.        
  352.         if(*(p2-1)=='.' && p2<numend) p2++;
  353.        
  354.         if(p2<numend) {
  355.             ovlstrcpy(p2,numend);numend=p2;
  356.         }
  357.         pp=numend-1;
  358.     }
  359. }
  360.  
  361. char *hname[]={"", "_1","_2","_3","_4","_5","_6","_7","_8"};
  362. #define hnameno (sizeof(hname)/sizeof(hname[0]))
  363.  
  364. void putheader(void)
  365. {
  366.     char hbuf[64];
  367.     char *p;
  368.     int i;
  369.    
  370.     inputptr=dynsetup(inputptr,inputend);
  371.     snprintf(inputptr,inputend-inputptr,"%s",header);
  372.     inputptr+=strlen(inputptr);
  373.     for(i=0;i<hnameno;i++) {
  374.         snprintf(hbuf,sizeof(hbuf),"w_%s_header%s",progname,hname[i]);
  375.         p=getenv(hbuf); if(p!=NULL && *p!=0) {
  376.             snprintf(parmbuf,parmlim,"%s",p);
  377.             check_parm(parmbuf);
  378.             snprintf(inputptr,inputend-inputptr,"%s\n",parmbuf);
  379.             inputptr+=strlen(inputptr);
  380.         }
  381.     }
  382. }
  383.  
  384. void putparm(void)
  385. {
  386.     snprintf(parmbuf,parmlim,"%s",parm); check_parm(parmbuf);
  387.     snprintf(inputptr,inputend-inputptr,stringprinter,startstring);
  388.     inputptr+=strlen(inputptr);
  389.     snprintf(inputptr,inputend-inputptr,"%s",parmbuf);
  390.     inputptr+=strlen(inputptr);
  391.     if(inputptr<inputend && inputptr>inputbuf && inputptr[-1]!='\n')
  392.       *inputptr++='\n';
  393.     snprintf(inputptr,inputend-inputptr,stringprinter,endstring);
  394.     inputptr+=strlen(inputptr);
  395.     *inputptr=0;
  396. }
  397.  
  398.         /* general first preparation */
  399. void prepare1(void)
  400. {
  401.     char *p, buf[256];
  402.     int i;
  403.     unsigned int r[4];
  404.     struct timeval t;
  405.  
  406.     parm=getenv("wims_exec_parm");
  407.         /* nothing to do if no calling parameter */
  408.     if(parm==NULL || *parm==0) exit(0);
  409.     inputptr=inputbuf; inputend=inputbuf+inputlim-1;
  410.     multiexec_random=getenv("multiexec_random");
  411.     if(multiexec_random==NULL) multiexec_random="";
  412.     if(*parm && strcmp(parm,multiexec_random)==0) {
  413.         multiexec=1;
  414.         getstdin(); parm=stdinbuf;
  415.         if(parm[0]==0) exit(0);
  416.     }
  417.     if(pipe(pipe_in)<0 || pipe(pipe_out)<0) {
  418.         fprintf(stderr,"%s: unable to create pipe.\n",progname);
  419.         exit(1);
  420.     }
  421. /*    i=fcntl(pipe_in[1],F_GETFL); if(i>=0) fcntl(pipe_in[1],F_SETFL,i|O_NONBLOCK);
  422.     i=fcntl(pipe_out[0],F_GETFL); if(i>=0) fcntl(pipe_out[0],F_SETFL,i|O_NONBLOCK);
  423. */    tmp_dir=getenv("tmp_dir"); if(tmp_dir==NULL || *tmp_dir==0) tmp_dir=tmpdir;
  424.     setenv("TMPDIR",tmp_dir,1);
  425.     mypid=getpid();
  426.     gettimeofday(&t,NULL); seed=t.tv_usec*t.tv_sec+mypid;
  427.     srandom(seed);
  428.     for(i=0;i<4;i++) r[i]=random();
  429.     snprintf(startstring,sizeof(startstring),
  430.              "Start line %s %u %u %u %u",progname,r[0],r[1],r[2],r[3]);
  431.     snprintf(endstring,sizeof(endstring),
  432.              "End line %s %u %u %u %u",progname,r[0],r[1],r[2],r[3]);
  433.     snprintf(buf,sizeof(buf),"%s_command",progname); p=getenv(buf);
  434.     if(p!=NULL && *find_word_start(p)!=0) nameofcmd=find_word_start(p);
  435.     snprintf(cmdbuf,sizeof(cmdbuf),"%s",nameofcmd); nameofcmd=cmdbuf;
  436.     cmdparm=find_word_end(nameofcmd); if(*cmdparm) *cmdparm++=0;
  437.     cmdparm=find_word_start(cmdparm);
  438.     snprintf(pidfname,sizeof(pidfname),"%s/exec.pid",tmp_dir); addpid(mypid);
  439.     snprintf(inputfname,sizeof(inputfname),"%s/%s_input.%d",tmp_dir,progname,mypid);
  440.     snprintf(outputfname,sizeof(outputfname),"%s/%s_output.%d",tmp_dir,progname,mypid);
  441.     errorf=NULL;
  442.     inpf=inputfname; outpf=outputfname;
  443.     isabout=0; p=find_word_start(parm); i=strlen("about");
  444.     if(memcmp(p,"about",i)==0 && *find_word_start(p+i)==0) isabout=1;
  445.     p=getenv("multiexec_debug"); if(p && strcmp(p,"yes")==0) debug=1;
  446. }
  447.  
  448. void prepabout(char *cmd, char *outf, char *errf)
  449. {
  450.     write(pipe_in[1],cmd,strlen(cmd));
  451.     execredirected(nameofcmd,NULL,outf,errf,cmdparm);
  452. }
  453.  
  454.         /* read to aboutbuf. Returns content length. */
  455. int readabout(void)
  456. {
  457.     FILE *ff;
  458.     long int l;
  459.     char *p;
  460.    
  461.     aboutbuf[0]=0; ff=fopen(outputfname,"r");
  462.     if(ff!=NULL) {
  463.         fseek(ff,0,SEEK_END); l=ftell(ff); fseek(ff,0,SEEK_SET);
  464.         l=fread(aboutbuf,1,aboutlen-10,ff); fclose(ff);
  465.         if(l>0 && l<aboutlen) aboutbuf[l]=0; else aboutbuf[0]=0;
  466.         p=find_word_start(aboutbuf); if(p>aboutbuf) ovlstrcpy(aboutbuf,p);
  467.     }
  468.     return strlen(aboutbuf);
  469. }
  470.  
  471.         /* read result of execution */
  472. void readresult(void)
  473. {
  474.     FILE *ff;
  475.     char *p, *pp, *pe;
  476.  
  477.     if(debug) {
  478.         ff=fopen(outputfname,"a"); if(ff) {
  479.             fputs(obuf,ff); fclose(ff);
  480.         }
  481.     }
  482.     pe=NULL;
  483.     p=strstr(obuf,startstring);
  484.     if(p) {
  485.         p+=strlen(startstring);
  486.         pe=strstr(p,endstring);
  487.         if(pe) {
  488.             for(pp=pe-1; pp>=p && pp>pe-64 && *pp!='\n'; pp--);
  489.             if(pp>=p && *pp=='\n') pe=pp;
  490.         }
  491.     }
  492.     if(pe) {
  493.         *pe=0;
  494.         while((pe=strstr(p,startstring))!=NULL) {
  495.             p=pe+strlen(startstring);
  496.         }
  497.         output(p);
  498.     }
  499.     else {
  500.         if(mxpid>=0) {
  501.             if(kill(mxpid,0)<0 && errno==ESRCH) {       /* this doesn't work */
  502.                 fprintf(stderr,"%s not_INStalled",progname);
  503.             }
  504.             else {
  505.                 fprintf(stderr,"%s: execution error or time out.\n",progname);
  506.                 kill(mxpid,SIGKILL);
  507.             }
  508.             rmpid(mxpid); mxpid=-1;
  509.         }
  510.         else {
  511.             fprintf(stderr,"%s: aborted on earlier error.\n",progname);
  512.         }
  513.     }
  514.     if(multiexec && *multiexec_random) printf("%s\n",multiexec_random);
  515.     fflush(stdout);
  516. }
  517.  
  518. #ifdef linebyline1
  519.  
  520.         /* this one is for maxima but not used */
  521. void dopipes(void)
  522. {
  523.     long int i, l;
  524.     char *outptr;
  525.     char *p1, *p2, *pp, *pe;
  526.     struct timeval t;
  527.     fd_set rset;
  528.  
  529.     if(mxpid<0) {
  530.         *obuf=0; return;
  531.     }
  532.     outptr=obuf; pp=pe=NULL;
  533.     for(p1=ibuf; *p1; p1=p2) {
  534.         p2=strchr(p1,'\n'); if(p2) p2++; else p2=p1+strlen(p1);
  535.         write(pipe_in[1],p1,p2-p1);
  536.        
  537.         if(strstr(p1,startstring)!=NULL) iactive=1;
  538.         reread:
  539.         l=read(fifofd2,lp,MAX_LINELEN-(lp-lbuf));
  540.         if(!iactive) continue;
  541.         if(l<0 || l>MAX_LINELEN-(lp-lbuf)) l=0;
  542.         lp+=l; *lp=0;
  543.         if(active==0) {
  544.             char *pp;
  545.             pp=strstr(lbuf,startstring);
  546.             if(pp!=NULL) {
  547.                 active=1; ovlstrcpy(lbuf,pp); lp=lbuf+strlen(lbuf);
  548.             }
  549.         }
  550.         if(active!=0 && strstr(lbuf,linebyline)!=NULL) {
  551.             fprintf(ff,"%s",lbuf); lp=lbuf;
  552.             if(strstr(lbuf,endstring)!=NULL) {lbuf[0]=0; active=-1; break;}
  553.             lbuf[0]=0; continue;
  554.         }
  555.         /* time out allowance between each silence */
  556.         t.tv_sec=3; t.tv_usec=400000;
  557.         i=select(fifofd2+1,&rset,NULL,NULL,&t);
  558.         if(i>0) goto reread;
  559.         else break; /* time out or error */
  560.     }
  561. }
  562.  
  563. #else
  564.  
  565. void dopipes(void)
  566. {
  567.     long int i, k, l;
  568.     int interval=20000; /* in microseconds */
  569.     int timeout =300;   /* in intervals */
  570.     char *outptr;
  571.     char *p1, *p2, *inp, *pw;
  572.     struct timeval t;
  573.     fd_set rset, wset;
  574.  
  575.     *obuf=0; if(mxpid<0) return;
  576.     outptr=obuf; inp=inputbuf; k=0;
  577.     while(inp<inputptr) {
  578.         t.tv_sec=0; t.tv_usec=interval;
  579.         FD_ZERO(&rset); FD_SET(pipe_out[0],&rset);
  580.         FD_ZERO(&wset); FD_SET(pipe_in[1],&wset);
  581.         i=pipe_out[0]; if(i<pipe_in[1]) i=pipe_in[1];
  582.         i=select(i+1,&rset,&wset,NULL,&t);
  583.         if(i<=0) {
  584.             k++; if(k>=timeout) return; /* time out */
  585.         }
  586.         if(FD_ISSET(pipe_in[1],&wset)) {
  587.                 /* Writing must be line by line, for otherwise
  588.                  * it will block when the input is large. */
  589.             for(pw=inp; pw<inputptr && *pw!='\n'; pw++);
  590.             if(pw<inputptr) pw++;
  591.             l=write(pipe_in[1],inp,pw-inp);
  592.             if(l<0 || l>inputptr-inp) return;
  593.             inp+=l;
  594.         }
  595.         if(FD_ISSET(pipe_out[0],&rset)) {
  596.             l=read(pipe_out[0],outptr,fsizelim-(outptr-obuf)-1);
  597.             if(l<=0 || l>fsizelim-(outptr-obuf)-1) {
  598.                 *outptr=0; break;
  599.             }
  600.             outptr+=l; *outptr=0;
  601.         }
  602.     }
  603.     p2=NULL;
  604.     p1=strstr(obuf,startstring);
  605.     if(p1) {
  606.         p1+=strlen(startstring);
  607.         p2=strstr(p1,endstring);
  608.     }
  609.     while(!p2) {
  610.         t.tv_sec=0; t.tv_usec=interval;
  611.         FD_ZERO(&rset); FD_SET(pipe_out[0],&rset);
  612.         i=select(pipe_out[0]+1,&rset,NULL,NULL,&t);
  613.         if(i>0) {
  614.             l=read(pipe_out[0],outptr,fsizelim-(outptr-obuf)-1);
  615.             if(l<=0 || l>fsizelim-(outptr-obuf)-1) {
  616.                 *outptr=0; break;
  617.             }
  618.             outptr+=l; *outptr=0;
  619.         }
  620.         else {
  621.             k++; if(k>=timeout) break;
  622.         }
  623.         if(!p1) {
  624.             p1=strstr(obuf,startstring);
  625.             if(p1) p1+=strlen(startstring);
  626.         }
  627.         if(p1) p2=strstr(p1,endstring);
  628.     }
  629. }
  630.  
  631. #endif
  632.  
  633. void run(void)
  634. {
  635.     FILE *ff;
  636.    
  637.     if(isabout) {about(); goto end;}
  638.     execredirected(nameofcmd,NULL,NULL,NULL,cmdparm);
  639.     putheader();
  640.     obuf=xmalloc(fsizelim); if(!obuf) return;
  641.     rep:
  642.     putparm();
  643.     if(!multiexec) {
  644.         snprintf(inputptr,inputend-inputptr,"%s",quitstring);
  645.         inputptr+=strlen(inputptr);
  646.     }
  647.     if(debug) {
  648.         ff=fopen(inputfname,"a"); if(ff) {
  649.             fputs(inputbuf,ff); fclose(ff);
  650.         }
  651.     }
  652.     dopipes();
  653.     readresult();
  654.     if(multiexec) {
  655.         getstdin(); inputptr=inputbuf; inputbuf[0]=0;
  656.         goto rep;
  657.     }
  658.     end: if(strstr(tmp_dir,"tmp/sessions/")==NULL) {
  659.         unlink(inputfname); unlink(outputfname);
  660.     }
  661.     free(obuf); rmpid(mypid);
  662.     if(mxpid>0) {kill(mxpid,SIGKILL); rmpid(mxpid);}
  663. }
  664.  
  665.