Subversion Repositories wimsdev

Rev

Rev 8155 | Rev 11125 | 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. void calc_itemof(char *p);
  19. void calc_rowof(char *p);
  20. void calc_columnof(char *p);
  21.  
  22. /* It is this routine which uses print_precision. */
  23. void float2str(double d, char *p)
  24. {
  25.     char buf[16];
  26.     int i;
  27.     if(d==0) {
  28.      ovlstrcpy(p,"0"); return;
  29.     }
  30.     if(!isfinite(d)) { /* isinf, isnan possibly not available */
  31.      if (d == d) ovlstrcpy(p, (d > 0)? "Inf": "-Inf"); else ovlstrcpy(p,"NaN");
  32.      return;
  33.     }
  34.     if(d<1000000 && d>-1000000 && d==floor(d)) {
  35.      mystrncpy(p,int2str(d),MAX_LINELEN); return;
  36.     }
  37.     i=print_precision;
  38.     if(i<2) i=2; if(i>32) i=32;  /* Simple limitation. */
  39.     buf[0]='%';buf[1]='.';
  40.     if(i>=10) {
  41.      buf[2]='0'+i/10; buf[3]='0'+i%10; buf[4]='g'; buf[5]=0;
  42.     }
  43.     else {
  44.      buf[2]='0'+i; buf[3]='g'; buf[4]=0;
  45.     }
  46.     snprintf(p,MAX_LINELEN,buf,(double) d);
  47.     if(isspace(*p)) ovlstrcpy(p,find_word_start(p));
  48. }
  49.  
  50. /* substitute variable names by their environment strings
  51.  * The buffer pointed to by p must have enough space
  52.  * (defined by MAX_LINELEN).
  53.  */
  54. char *substit(char *p)
  55. {
  56.     char *pp, *p2, *ev;
  57.     char *oldlast, *oldnext, *oldend, *newend;
  58.     char buf[MAX_LINELEN+1], oldbuf[MAX_LINELEN+1];
  59.     int ln;
  60.  
  61.     if((p2=strchr(p,'$'))==NULL) return p;
  62.     if(substnest>SUBST_LIMIT) module_error("subst_exceeded");
  63.     ln=strlen(p); if(ln>=MAX_LINELEN) goto too_long;
  64.     memmove(oldbuf,p,ln); oldbuf[ln]=0; oldend=oldbuf+ln;
  65.     newend=p; oldlast=oldnext=oldbuf;
  66.     for(pp=oldbuf+(p2-p); pp!=NULL; pp=strchr(pp,'$')) {
  67.      if(*(pp+1)=='$') { /* escaped dollar sign */
  68.          pp++; if(newend-p+(pp-oldlast)>=MAX_LINELEN) goto too_long;
  69.          memmove(newend,oldlast,pp-oldlast); newend+=pp-oldlast;
  70.          pp++; oldlast=pp; continue;
  71.      }
  72.      switch(*(pp+1)) {
  73.          case 0: {
  74.           *pp=0; oldend--; goto end;
  75.          }
  76.          case '(': {
  77.           p2=find_matching(pp+2,')');
  78.           if(p2==NULL) {
  79.               module_error("unmatched_parentheses");
  80.               *p=0; return p;
  81.           }
  82.           *p2++=0; oldnext=p2; memmove(buf,pp+2,p2-pp-2);
  83.           substnest++; substit(buf); substnest--;
  84.           break;
  85.          }
  86.  
  87.          case '[': {
  88.           double d;
  89.           p2=find_matching(pp+2,']');
  90.           if(p2==NULL) {
  91.               module_error("unmatched_parentheses");
  92.               *p=0; return p;
  93.           }
  94.           *p2=0; ovlstrcpy(buf,pp+2); oldnext=p2+1;
  95.           substnest++; substit(buf); substnest--;
  96.           d=evalue(buf); float2str(d,buf);
  97.           goto replace;
  98.          }
  99.  
  100.          default: {
  101.           for(p2=pp+1; myisalnum(*p2) || *p2=='_'; p2++);
  102.           oldnext=p2; memmove(buf,pp+1,p2-(pp+1)); buf[p2-(pp+1)]=0;
  103.           goto noarray;
  104.          }
  105.      }
  106.      if((p2=strchr(buf,'['))!=NULL) {
  107.          char *pt1, tbuf[MAX_LINELEN+1];
  108.          *p2++=0; pt1=find_matching(p2,']');
  109.          if(pt1==NULL) {buf[0]=0; goto replace;}
  110.          *pt1=0; pt1=strchr(p2,';');
  111.          if(pt1==NULL) {
  112.           if(*find_word_start(p2)==0) {*p2=0; goto noarray;}
  113.           snprintf(tbuf,sizeof(tbuf),"%s of $%s",p2,buf);
  114.           calc_itemof(tbuf); ovlstrcpy(buf,tbuf); goto replace;
  115.          }
  116.          else {
  117.           *pt1++=0; p2=find_word_start(p2);
  118.           if(*p2) {
  119.               snprintf(tbuf,sizeof(tbuf),"%s of $%s",p2,buf);
  120.               calc_rowof(tbuf);
  121.           }
  122.           else {
  123.               snprintf(tbuf,sizeof(tbuf),"$%s",buf); substit(tbuf);
  124.           }
  125.           if(*find_word_start(pt1)) {
  126.               snprintf(buf,sizeof(buf),"%s of %s",pt1,tbuf);
  127.               calc_columnof(buf); goto replace;
  128.           }
  129.           else ovlstrcpy(buf,tbuf);
  130.           goto replace;
  131.          }
  132.      }
  133.      noarray: ev=getvar(buf); ln=getvar_len;
  134.      if(ev==NULL) ev=""; if(strchr(ev,'$')==NULL) goto rep2;
  135.      memmove(buf,ev,ln); buf[ln]=0;
  136.      substnest++; substit(buf); substnest--;
  137.      replace: ev=buf; ln=strlen(ev);
  138.      rep2:
  139.      if(pp>oldlast) {
  140.          if(newend-p+(pp-oldlast)>=MAX_LINELEN) goto too_long;
  141.          memmove(newend,oldlast,pp-oldlast); newend+=pp-oldlast;
  142.      }
  143.      if(ln>0) {
  144.          if((newend-p)+ln>=MAX_LINELEN) goto too_long;
  145.          memmove(newend,ev,ln); newend+=ln;
  146.      }
  147.      pp=oldlast=oldnext;
  148.      continue;
  149.     }
  150.     end:
  151.     if(oldlast<oldend) {
  152.      if(newend-p+(oldend-oldlast)>=MAX_LINELEN) goto too_long;
  153.      memmove(newend,oldlast,oldend-oldlast); newend+=oldend-oldlast;
  154.     }
  155.     *newend=0; return p;
  156.     too_long: user_error("cmd_output_too_long"); return NULL;
  157. }
  158.  
  159. enum {for_in, for_from};
  160. struct {
  161.     char var[MAX_NAMELEN+1];
  162.     int type;
  163.     double list[MAX_VALUE_LIST];
  164.     char *pos[MAX_VALUE_LIST];
  165.     double from, to, step;
  166. } forstruct;
  167.  
  168. int for_getvar(char *p)
  169. {
  170.     char *vp, buf[MAX_LINELEN+1];
  171.     mystrncpy(buf,p,sizeof(buf)); substit(buf);
  172.     vp=find_word_start(buf); for(p=vp; myisalnum(*p) || *p=='_'; p++);
  173.     *p=0; if(p-vp<=0 || p-vp>MAX_NAMELEN) return -1;
  174.     mystrncpy(forstruct.var,vp,sizeof(forstruct.var));
  175.     return 0;
  176. }
  177.  
  178. /* If bufp=NULL then numeric. Else string-wise.
  179.  * returns 0 if success,
  180.  * -1 if syntax error, 1 if bad values
  181.  */
  182. int cutfor(char *p, char *bufp)
  183. {
  184.     char *eqp, *fromp, *top, *stepp, *inp;
  185.  
  186.     p=find_word_start(p);
  187.     inp=find_word_start(find_word_end(p));
  188.     if(wordchr(inp,"in")==inp) {
  189.      char buf[MAX_LINELEN+1], *bp;
  190.      int i;
  191.      double d;
  192.      *inp=0; inp+=strlen("in");
  193.      forin: inp=find_word_start(inp); forstruct.type=for_in;
  194.      if(for_getvar(p)) return -1;
  195.      if(bufp!=NULL) bp=bufp; else bp=buf;
  196.      mystrncpy(bp,inp,MAX_LINELEN); substit(bp);
  197.      strip_trailing_spaces(bp);
  198.      if(bp[0]==0) {
  199.          forstruct.from=0; forstruct.to=-1; forstruct.step=1; return 0;
  200.      }
  201.      for(i=0, inp=bp; i<MAX_VALUE_LIST && inp!=NULL; inp=top) {
  202.          top=strparchr(inp,','); if(top) *top++=0;
  203.          if(bufp==NULL) {
  204.           d=evalue(inp); if(isfinite(d)) forstruct.list[i++]=d;
  205.           else return 1;
  206.          }
  207.          else {
  208.           strip_trailing_spaces(inp);
  209.           forstruct.pos[i++]=find_word_start(inp);
  210.          }
  211.      }
  212.      forstruct.from=0; forstruct.to=i-1; forstruct.step=1; return 0;
  213.     }
  214.     top=wordchr(p,"to"); if(top==NULL) {
  215.      inp=strchr(p,'='); if(inp==NULL) return -1;
  216.      *inp++=0; goto forin;
  217.     }
  218.     *top=0; top+=strlen("to");
  219.     stepp=wordchr(top,"step"); if(stepp!=NULL) {
  220.      *stepp=0; stepp+=strlen("step"); forstruct.step=evalue(stepp);
  221.     }
  222.     else forstruct.step=1;
  223.     forstruct.to=evalue(top); forstruct.type=for_from;
  224.     eqp=strchr(p,'='); fromp=wordchr(p,"from"); inp=wordchr(p,"in");
  225.     if(eqp!=NULL && (fromp==NULL || eqp<fromp)) {
  226.      *eqp++=0; fromp=eqp;
  227.     }
  228.     else {
  229.      if(fromp==NULL) return -1;
  230.      *fromp=0; fromp+=strlen("from");
  231.     }
  232.     forstruct.from=evalue(fromp);
  233.     if(for_getvar(p)) return -1;
  234.     if(!isfinite(forstruct.from+forstruct.to+forstruct.step)) return 1;
  235.     else return 0;
  236. }
  237.  
  238.