Subversion Repositories wimsdev

Rev

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