Back to index

wims  3.65+svn20090927
outexec.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* these are patches for rawmath.c */
00019 char *getvar(char *p) {return NULL;}
00020 void setvar(char *p, char *v) {return;}
00021 
00022 char *exec_if(char *p)
00023 {
00024     char *p1, *p2, *p3, *p4, *p5, *p6, *pp;
00025     char buf[MAX_LINELEN+1];
00026     p1=find_word_start(p); if(*p1!='{') return p;
00027     p2=find_matching(p1+1,'}'); if(p2==NULL) return p;
00028     p3=find_word_start(p2+1); if(*p3!='{') return p;
00029     p4=find_matching(p3+1,'}'); if(p4==NULL) return p;
00030     *p2=0; snprintf(buf,sizeof(buf),"%s",p1+1);
00031     for(pp=strchr(buf,'\\'); pp!=NULL; pp=strchr(pp+1,'\\')) {
00032        if(isalnum(*(pp+1))) string_modify(buf,pp,pp+1,"$m_");
00033     }
00034     fprintf(outf," \n!if %s \n$()",buf);
00035     p5=find_word_start(p4+1);
00036     if(*p5=='{' && (p6=find_matching(p5+1,'}'))!=NULL) {
00037        *p4=elsechar; *p5=' '; *p6=endifchar;
00038     }
00039     else *p4=endifchar;
00040     return p3+1;
00041 }
00042 
00043 char *exec_for(char *p)
00044 {
00045     char *p1, *p2, *p3, *p4, *pp;
00046     char buf[MAX_LINELEN+1];
00047     p1=find_word_start(p); if(*p1!='{') return p;
00048     p2=find_matching(p1+1,'}'); if(p2==NULL) return p;
00049     p3=find_word_start(p2+1); if(*p3!='{') return p;
00050     p4=find_matching(p3+1,'}'); if(p4==NULL) return p;
00051     *p2=0; snprintf(buf,sizeof(buf),"%s",p1+1);
00052     for(pp=strchr(buf,'\\'); pp!=NULL; pp=strchr(pp+1,'\\')) {
00053        if(isalnum(*(pp+1))) string_modify(buf,pp,pp+1,"$m_");
00054     }
00055     fprintf(outf," \n!for m_%s \n$()",find_word_start(buf));
00056     *p4=nextchar;
00057     return p3+1;    
00058 }
00059 
00060 void process_formula(char *p)
00061 {
00062     char *p3, bf[MAX_LINELEN+1];
00063     
00064     if(strlen(p)>=MAX_LINELEN) error("formula too long");
00065     while((p3=strstr(p,"&lt;"))!=NULL) memmove(p3," <  ",4);
00066     while((p3=strstr(p,"&gt;"))!=NULL) memmove(p3," >  ",4);
00067     for(p3=strchr(p,'\n'); p3!=NULL; p3=strchr(p3,'\n')) *p3=' ';
00068     snprintf(bf,sizeof(bf),"%s",p); 
00069     if(strchr(bf,'\\')==NULL && strchr(bf,'}')==NULL && strlen(bf)>2) {
00070        for(p3=strstr(bf,".."); p3!=NULL; p3=strstr(p3,"..")) {
00071            if(*(p3+2)=='.' || *(p3+2)==',') {
00072               do p3++; while(*p3=='.'); continue;
00073            }
00074            *p3=','; *(p3+1)=' ';
00075        }
00076     }
00077     fprintf(outf,"\n!insmath %s\n$()",bf);
00078 }
00079 
00080 void out_exec(char *s1, char *s2)
00081 {
00082     char *ps, *p, *pp, *pe, *pp2, *pe2, *pt, c;
00083     char *p2, *p3;
00084     if(s2) fprintf(outf,"\n!exit\n\n:%s\n$()",s2);
00085     ps=s1;
00086     for(p=ps;*p;p++) {
00087        if(*p==nextchar) {
00088            *p=0;
00089            fprintf(outf,"%s \n!next\n$()",ps); ps=p+1; continue;
00090        }
00091        if(*p==elsechar) {
00092            *p=0;
00093            fprintf(outf,"%s \n!else\n$()",ps); ps=p+1; continue;
00094        }
00095        if(*p==endifchar) {
00096            *p=0;
00097            fprintf(outf,"%s \n!endif\n$()",ps); ps=p+1; continue;
00098        }
00099        if(*p!='\\') continue;
00100        c=*(p+1);
00101        if(isalnum(c)) {
00102               /* exit */
00103            if(strncmp(p+1,"exit",4)==0 && !isalnum(*(p+5))) {
00104               *p=0; fprintf(outf,"%s\n!exit\n",ps); p+=5; ps=p;
00105               continue;
00106            }
00107               /* for */
00108            if(strncmp(p+1,"for",3)==0 && *find_word_start(p+4)=='{') {
00109               char *pt;
00110               *p=0; fprintf(outf,"%s",ps); p++; ps=p;
00111               pt=exec_for(p+3); if(pt>p+3) {p=pt-1;ps=pt;}
00112               continue;
00113            }
00114               /* if */
00115            if(strncmp(p+1,"if",2)==0 && *find_word_start(p+3)=='{') {
00116               char *pt;
00117               *p=0; fprintf(outf,"%s",ps); p++; ps=p;
00118               pt=exec_if(p+2); if(pt>p+2) {p=pt-1;ps=pt;}
00119               continue;
00120            }
00121               /* draw */
00122            if(strncmp(p+1,"draw",4)==0 && *find_word_start(p+5)=='{') {
00123               pe=pp2=pe2="";
00124               pp=find_word_start(p+5);
00125               if(*pp) pe=find_matching(pp+1,'}');
00126               if(pe) pp2=find_word_start(pe+1); else continue;
00127               if(pp2) pe2=find_matching(pp2+1,'}'); else continue;
00128               if(pe2 && *pp2=='{' && *pe2=='}') {
00129                   pp++; pp2++; *p=*pe=*pe2=0;
00130                   while((pt=strstr(pp2,"$val1/"))!=NULL) 
00131                     strcpy(pt,pt+strlen("$val1/"));
00132                   fprintf(outf,"%s \n\
00133 !read oef/draw.phtml %s \\\n%s \n$()", ps,pp,pp2);
00134                   ps=p=pe2; ps++; continue;
00135               }
00136            }
00137               /* img */
00138            if(strncmp(p+1,"img",3)==0 && *find_word_start(p+4)=='{') {
00139               pe=pp2=NULL;
00140               pp=find_word_start(p+4);
00141               if(*pp=='{') pe=find_matching(pp+1,'}');
00142               if(pe) pp2=find_word_start(pe+1); else continue;
00143               pe2=pe;
00144               if(*pp2=='{') {
00145                   pe2=find_matching(++pp2,'}');
00146                   if(pe2) *pe2=0;
00147               }
00148               else pp2="";
00149               if(*pp=='{' && *pe=='}') {
00150                   pp++; *p=*pe=0;
00151                   fprintf(outf,"%s \n\
00152 !read oef/img.phtml %s %s \n$()", ps,pp,pp2);
00153                   ps=p=pe2; ps++; continue;
00154               }
00155            }
00156            if(strncmp(p+1,"embed",5)==0 && *find_word_start(p+6)=='{') {
00157               pe=pp2=pe2="";
00158               pp=find_word_start(p+6);
00159               if(*pp) pe=find_matching(pp+1,'}');
00160               if(pe && *pp=='{' && *pe=='}') {
00161                   pp++; *p=*pe=0;
00162                   fprintf(outf,"%s \n\
00163 !read oef/embed.phtml %s \n$()", ps,pp);
00164                   ps=p=pe; ps++; embedcnt++; continue;
00165               }
00166            }
00167            if(strncmp(p+1,"special",7)==0 && *find_word_start(p+8)=='{') {
00168               pe=pp2=pe2="";
00169               pp=find_word_start(p+8);
00170               if(*pp) pe=find_matching(pp+1,'}');
00171               if(pe && *pp=='{' && *pe=='}') {
00172                   pp++; *p=*pe=0;
00173                   fprintf(outf,"%s \n\
00174 !read oef/special.phtml %s \n$()", ps,pp);
00175                   ps=p=pe; ps++; embedcnt++; continue;
00176               }
00177            }
00178            *p++=0; fprintf(outf,"%s$m_",ps); ps=p; continue;
00179        }
00180        if(c=='\\') {
00181            strcpy(p,p+1); continue;
00182        }
00183        if(c=='(') {
00184            p2=find_matching(p+2,')'); p3=strstr(p,"\\)");
00185            if((p2==NULL && p3!=NULL) ||
00186               (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
00187            if(p2==NULL) continue;
00188            *p++=0; fprintf(outf,"%s",ps);
00189            *p2=0; if(*(p2-1)=='\\') *(p2-1)=0;
00190            process_formula(p+1); 
00191            formulaend: p=p2; ps=p+1;
00192            continue;
00193        }
00194        if(c=='{') {
00195            p2=find_matching(p+2,'}'); p3=strstr(p,"\\}");
00196            if((p2==NULL && p3!=NULL) ||
00197               (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
00198            if(p2==NULL) continue;
00199            *p++=0; fprintf(outf,"%s",ps);
00200            *p2=0; process_formula(p+1); 
00201            goto formulaend;
00202        }
00203     }
00204     fprintf(outf,"%s\n$()",ps);
00205 }
00206