Subversion Repositories wimsdev

Rev

Rev 8135 | Rev 8195 | 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. #include "../Lib/libwims.h"
  19. #include "oef2wims.h"
  20. /* these are patches for rawmath.c */
  21. /*
  22. char *getvar(char *p) {return NULL;}
  23. void setvar(char *p, char *v) {return;}
  24. */
  25. char *exec_if(char *p)
  26. {
  27.     char *p1, *p2, *p3, *p4, *p5, *p6, *pp;
  28.     char buf[MAX_LINELEN+1];
  29.     p1=find_word_start(p); if(*p1!='{') return p;
  30.     p2=find_matching(p1+1,'}'); if(p2==NULL) return p;
  31.     p3=find_word_start(p2+1); if(*p3!='{') return p;
  32.     p4=find_matching(p3+1,'}'); if(p4==NULL) return p;
  33.     *p2=0; snprintf(buf,sizeof(buf),"%s",p1+1);
  34.     for(pp=strchr(buf,'\\'); pp!=NULL; pp=strchr(pp+1,'\\')) {
  35.       if(isalnum(*(pp+1))) string_modify(buf,pp,pp+1,"$m_");
  36.     }
  37.     fprintf(outf," \n!if %s \n$()",buf);
  38.     p5=find_word_start(p4+1);
  39.     if(*p5=='{' && (p6=find_matching(p5+1,'}'))!=NULL) {
  40.       *p4=elsechar; *p5=' '; *p6=endifchar;
  41.     }
  42.     else *p4=endifchar;
  43.     return p3+1;
  44. }
  45.  
  46. char *exec_for(char *p)
  47. {
  48.     char *p1, *p2, *p3, *p4, *pp;
  49.     char buf[MAX_LINELEN+1];
  50.     p1=find_word_start(p); if(*p1!='{') return p;
  51.     p2=find_matching(p1+1,'}'); if(p2==NULL) return p;
  52.     p3=find_word_start(p2+1); if(*p3!='{') return p;
  53.     p4=find_matching(p3+1,'}'); if(p4==NULL) return p;
  54.     *p2=0; snprintf(buf,sizeof(buf),"%s",p1+1);
  55.     for(pp=strchr(buf,'\\'); pp!=NULL; pp=strchr(pp+1,'\\')) {
  56.       if(isalnum(*(pp+1))) string_modify(buf,pp,pp+1,"$m_");
  57.     }
  58.     fprintf(outf," \n!for m_%s \n$()",find_word_start(buf));
  59.     *p4=nextchar;
  60.     return p3+1;
  61. }
  62. /* process math formula inside \( ) or \( \) */
  63. void process_formula(char *p)
  64. {
  65.     char *p3, bf[MAX_LINELEN+1];
  66.  
  67.     if(strlen(p)>=MAX_LINELEN) error("formula too long");
  68.     while((p3=strstr(p,"&lt;"))!=NULL) memmove(p3," <  ",4);
  69.     while((p3=strstr(p,"&gt;"))!=NULL) memmove(p3," >  ",4);
  70.     for(p3=strchr(p,'\n'); p3!=NULL; p3=strchr(p3,'\n')) *p3=' ';
  71.     snprintf(bf,sizeof(bf),"%s",p);
  72.     if(strchr(bf,'\\')==NULL && strchr(bf,'}')==NULL && strlen(bf)>2) {
  73.       for(p3=strstr(bf,".."); p3!=NULL; p3=strstr(p3,"..")) {
  74.           if(*(p3+2)=='.' || *(p3+2)==',') {
  75.             do p3++; while(*p3=='.'); continue;
  76.           }
  77.           *p3=','; *(p3+1)=' ';
  78.       }
  79.     }
  80.     fprintf(outf,"\n!insmath %s\n$()",bf);
  81. }
  82.  
  83. void out_exec(char *s1, char *s2)
  84. {
  85.     char *ps, *p, *pp, *pe, *pp2, *pe2, *pt, c;
  86.     char *p2, *p3;
  87.     if(s2) fprintf(outf,"\n!exit\n\n:%s\n$()",s2);
  88.     ps=s1;
  89.     for(p=ps;*p;p++) {
  90.       if(*p==nextchar) {
  91.           *p=0;
  92.           fprintf(outf,"%s \n!next\n$()",ps); ps=p+1; continue;
  93.       }
  94.       if(*p==elsechar) {
  95.           *p=0;
  96.           fprintf(outf,"%s \n!else\n$()",ps); ps=p+1; continue;
  97.       }
  98.       if(*p==endifchar) {
  99.           *p=0;
  100.           fprintf(outf,"%s \n!endif\n$()",ps); ps=p+1; continue;
  101.       }
  102.       if(*p!='\\') continue;
  103.       c=*(p+1);
  104.       if(isalnum(c)) {
  105. /* exit */
  106.           if(strncmp(p+1,"exit",4)==0 && !isalnum(*(p+5))) {
  107.             *p=0; fprintf(outf,"%s\n!exit\n",ps); p+=5; ps=p;
  108.             continue;
  109.           }
  110. /* for */
  111.           if(strncmp(p+1,"for",3)==0 && *find_word_start(p+4)=='{') {
  112.             char *pt;
  113.             *p=0; fprintf(outf,"%s",ps); p++; ps=p;
  114.             pt=exec_for(p+3); if(pt>p+3) {p=pt-1;ps=pt;}
  115.             continue;
  116.           }
  117. /* if */
  118.           if(strncmp(p+1,"if",2)==0 && *find_word_start(p+3)=='{') {
  119.             char *pt;
  120.             *p=0; fprintf(outf,"%s",ps); p++; ps=p;
  121.             pt=exec_if(p+2); if(pt>p+2) {p=pt-1;ps=pt;}
  122.             continue;
  123.           }
  124. /* canvasdraw */
  125.           if(strncmp(p+1,"canvasdraw",10)==0 && *find_word_start(p+11)=='{') {
  126.             pe=pp2=pe2="";
  127.             pp=find_word_start(p+11);
  128.             if(*pp) pe=find_matching(pp+1,'}');
  129.             if(pe) pp2=find_word_start(pe+1); else continue;
  130.             if(pp2) pe2=find_matching(pp2+1,'}'); else continue;
  131.             if(pe2 && *pp2=='{' && *pe2=='}') {
  132.                 pp++; pp2++; *p=*pe=*pe2=0;
  133.                 while((pt=strstr(pp2,"$val1/"))!=NULL)
  134.                   ovlstrcpy(pt,pt+strlen("$val1/"));
  135.                 fprintf(outf,"%s \n\
  136. !read oef/canvasdraw.phtml %s \\\n%s \n$()", ps,pp,pp2);
  137.                 ps=p=pe2; ps++; continue;
  138.             }
  139.           }
  140.  
  141. /* draw */
  142.           if(strncmp(p+1,"draw",4)==0 && *find_word_start(p+5)=='{') {
  143.             pe=pp2=pe2="";
  144.             pp=find_word_start(p+5);
  145.             if(*pp) pe=find_matching(pp+1,'}');
  146.             if(pe) pp2=find_word_start(pe+1); else continue;
  147.             if(pp2) pe2=find_matching(pp2+1,'}'); else continue;
  148.             if(pe2 && *pp2=='{' && *pe2=='}') {
  149.                 pp++; pp2++; *p=*pe=*pe2=0;
  150.                 while((pt=strstr(pp2,"$val1/"))!=NULL)
  151.                   ovlstrcpy(pt,pt+strlen("$val1/"));
  152.                 fprintf(outf,"%s \n\
  153. !read oef/draw.phtml %s \\\n%s \n$()", ps,pp,pp2);
  154.                 ps=p=pe2; ps++; continue;
  155.             }
  156.           }
  157. /* img */
  158.           if(strncmp(p+1,"img",strlen("img"))==0 && *find_word_start(p+strlen("img")+1)=='{') {
  159.             pe=pp2=NULL;
  160.             pp=find_word_start(p+strlen("img")+1);
  161.             if(*pp=='{') pe=find_matching(pp+1,'}');
  162.             if(pe) pp2=find_word_start(pe+1); else continue;
  163.             pe2=pe;
  164.             if(*pp2=='{') {
  165.                 pe2=find_matching(++pp2,'}');
  166.                 if(pe2) *pe2=0;
  167.             }
  168.             else pp2="";
  169.             if(*pp=='{' && *pe=='}') {
  170.                 pp++; *p=*pe=0;
  171.                 fprintf(outf,"%s \n\
  172. !read oef/img.phtml %s %s \n$()", ps,pp,pp2);
  173.                 ps=p=pe2; ps++; continue;
  174.             }
  175.           }
  176. /* audio */
  177.           if(strncmp(p+1,"audio",strlen("audio"))==0 && *find_word_start(p+strlen("audio")+1)=='{') {
  178.             pe=pp2=NULL;
  179.             pp=find_word_start(p+strlen("audio")+1);
  180.             if(*pp=='{') pe=find_matching(pp+1,'}');
  181.             if(pe) pp2=find_word_start(pe+1); else continue;
  182.             pe2=pe;
  183.             if(*pp2=='{') {
  184.                 pe2=find_matching(++pp2,'}');
  185.                 if(pe2) *pe2=0;
  186.             }
  187.             else pp2="";
  188.             if(*pp=='{' && *pe=='}') {
  189.                 pp++; *p=*pe=0;
  190.                 fprintf(outf,"%s \n\
  191. !read oef/audio.phtml %s %s \n$()", ps,pp,pp2);
  192.                 ps=p=pe2; ps++; continue;
  193.             }
  194.           }
  195.           if(strncmp(p+1,"embed",5)==0 && *find_word_start(p+6)=='{') {
  196.             pe=pp2=pe2="";
  197.             pp=find_word_start(p+6);
  198.             if(*pp) pe=find_matching(pp+1,'}');
  199.             if(pe && *pp=='{' && *pe=='}') {
  200.                 pp++; *p=*pe=0;
  201.                 fprintf(outf,"%s \n\
  202. !read oef/embed.phtml %s \n$()", ps,pp);
  203.                 ps=p=pe; ps++; embedcnt++; continue;
  204.             }
  205.           }
  206.           if(strncmp(p+1,"special",7)==0 && *find_word_start(p+8)=='{') {
  207.             pe=pp2=pe2="";
  208.             pp=find_word_start(p+8);
  209.             if(*pp) pe=find_matching(pp+1,'}');
  210.             if(pe && *pp=='{' && *pe=='}') {
  211.                 pp++; *p=*pe=0;
  212.                 fprintf(outf,"%s \n\
  213. !read oef/special.phtml %s \n$()", ps,pp);
  214.                 ps=p=pe; ps++; embedcnt++; continue;
  215.             }
  216.           }
  217.           *p++=0; fprintf(outf,"%s$m_",ps); ps=p; continue;
  218.       }
  219.       if(c=='\\') {
  220.           ovlstrcpy(p,p+1); continue;
  221.       }
  222.       if(c=='(') {
  223.           p2=find_matching(p+2,')'); p3=strstr(p,"\\)");
  224.           if((p2==NULL && p3!=NULL) ||
  225.              (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
  226.           if(p2==NULL) continue;
  227.           *p++=0; fprintf(outf,"%s",ps);
  228.           *p2=0; if(*(p2-1)=='\\') *(p2-1)=0;
  229.           process_formula(p+1);
  230.           formulaend: p=p2; ps=p+1;
  231.           continue;
  232.       }
  233.       if(c=='{') {
  234.           p2=find_matching(p+2,'}'); p3=strstr(p,"\\}");
  235.           if((p2==NULL && p3!=NULL) ||
  236.              (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
  237.           if(p2==NULL) continue;
  238.           *p++=0; fprintf(outf,"%s",ps);
  239.           *p2=0; process_formula(p+1);
  240.           goto formulaend;
  241.       }
  242.     }
  243.     fprintf(outf,"%s\n$()",ps);
  244. }
  245.  
  246.