Subversion Repositories wimsdev

Rev

Rev 8400 | Rev 8850 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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