Subversion Repositories wimsdev

Rev

Rev 9227 | Rev 11123 | 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
 */
8135 bpr 17
#include "../Lib/libwims.h"
18
#include "oef2wims.h"
10 reyssat 19
int prepcnt;
2656 bpr 20
int ex_statement=0, ex_hint=0, ex_help=0, ex_solution=0, ex_latex=0;
8135 bpr 21
 
22
const size_t MAX_KEY_LEN=128;
10 reyssat 23
char vbuf_statement[MAX_LINELEN+1];
24
char vbuf_hint[MAX_LINELEN+1];
25
char vbuf_help[MAX_LINELEN+1];
26
char vbuf_solution[MAX_LINELEN+1];
9093 bpr 27
char vbuf_latex[MAX_LINELEN+1];
10 reyssat 28
 
7677 bpr 29
/* empty processor, template. */
10 reyssat 30
void empty(char *p[MAX_PARM]) {}
31
 
9095 bpr 32
void p_header(char *p[MAX_PARM])
3288 bpr 33
{
34
    p[0]=find_word_start(p[0]);
35
    if(strlen(p[0])>MAX_KEY_LEN) p[0][MAX_KEY_LEN]=0;
9095 bpr 36
    fprintf(outf,"%s=%s\n", p[1], p[0]);
3288 bpr 37
}
38
 
10 reyssat 39
void p_computeanswer(char *p[MAX_PARM])
7677 bpr 40
{
10 reyssat 41
    p[0]=find_word_start(p[0]);
42
    *find_word_end(p[0])=0;
43
    if(strcasecmp(p[0],"yes"))
44
      fprintf(outf,"computeanswer=no\n");
45
    else fprintf(outf,"computeanswer=%s\n",p[0]);
46
}
47
 
48
void p_precision(char *p[MAX_PARM])
49
{
50
    int pr;
51
    pr=atoi(p[0]);
52
    if(pr<0 || pr>100000000) return;
53
    fprintf(outf,"precision=%d\n",pr);
54
}
9227 bpr 55
void p_gen2(char *name_gen, char *p[MAX_PARM])
7677 bpr 56
{
9227 bpr 57
    char vbuf_gen[MAX_LINELEN+1];
1105 bpr 58
    if(p==NULL) return;
9227 bpr 59
    snprintf(vbuf_gen,sizeof(vbuf_gen),"%s",p[0]); subst(vbuf_gen);
60
    singlespace(vbuf_gen);
9229 bpr 61
    fprintf(outf,"%s=%s\n", name_gen, vbuf_gen);
1105 bpr 62
}
9227 bpr 63
void p_css(char *p[MAX_PARM]) {p_gen2("oefcss", p);}
64
void p_credits(char *p[MAX_PARM]){p_gen2("credits", p);}
9229 bpr 65
void p_description(char *p[MAX_PARM]){p_gen2("oefdescription", p);}
66
void p_observation(char *p[MAX_PARM]){p_gen2("oefobservation", p);}
4810 bpr 67
 
10 reyssat 68
void p_wims(char *p[MAX_PARM])
69
{
70
    char vbuf[MAX_LINELEN+1];
71
    snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf);
7677 bpr 72
/* the second condition could be removed?
73
 * To be very careful! */
10 reyssat 74
    if(strchr(vbuf,'=')==NULL || strchr(vbuf,'!')!=NULL) return;
7677 bpr 75
    fprintf(outf,"%s\n",vbuf);
10 reyssat 76
}
77
 
78
void p_mdef(char *p[MAX_PARM])
79
{
80
    char vbuf[MAX_LINELEN+1];
81
    if(wordchr(mdef,p[0])==NULL) return;
82
    snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf);
7677 bpr 83
/* the second condition could be removed?
84
 * To be very careful! */
10 reyssat 85
    if(strchr(vbuf,'!')!=NULL) return;
86
    fprintf(outf,"m_%s=%s\n",p[0],vbuf);
87
}
88
 
89
void p_range(char *p[MAX_PARM])
90
{
91
    double left, right;
92
    char *pp;
93
    pp=strstr(p[0],"..");
94
    if(pp==NULL) return;
95
    *pp=0;pp+=strlen("..");
96
    left=atof(p[0]); right=atof(pp);
97
    if(left>=right-1E-50 || left<-1E50 || right>1E50) return;
98
    fprintf(outf,"leftrange=%f\nrightrange=%f\n",left,right);
99
}
100
 
101
void p_language(char *p[MAX_PARM])
7677 bpr 102
{
10 reyssat 103
    p[0]=find_word_start(p[0]);
104
    *find_word_end(p[0])=0;
7677 bpr 105
    if(strlen(p[0])==2) fprintf(outf,"language=%s\n",p[0]);
10 reyssat 106
}
107
 
108
void p_statement(char *p[MAX_PARM])
109
{
110
    if(ex_statement<0) return;
111
    if(ex_statement>0 || p==NULL) {
7677 bpr 112
      out_exec(vbuf_statement,"question");
113
      ex_statement=-1; return;
10 reyssat 114
    }
115
    if(p==NULL) return;
116
    snprintf(vbuf_statement,sizeof(vbuf_statement),"%s",p[0]);
117
    subst(vbuf_statement);
118
    if(strcmp(format,"html")!=0) {
7677 bpr 119
      fprintf(outf,"question=!nosubst %s\n",vbuf_statement);
120
      ex_statement=-1;
10 reyssat 121
    }
122
    else {
7677 bpr 123
      fprintf(outf,"question=%s\n",executed_str);
124
      ex_statement=1;
10 reyssat 125
    }
126
}
127
 
9093 bpr 128
void p_gen(int *ex_gen, char vbuf_gen[], char *name_gen, char *p[MAX_PARM])
10 reyssat 129
{
9093 bpr 130
    if(*ex_gen<0) return;
131
    if(*ex_gen>0 || p==NULL) {
132
      out_exec(vbuf_gen, name_gen);
133
      *ex_gen=-1; return;
10 reyssat 134
    }
9093 bpr 135
    snprintf(vbuf_gen,MAX_LINELEN,"%s",p[0]); subst(vbuf_gen);
136
    if(strchr(vbuf_gen,'\\')!=NULL) {
137
      fprintf(outf,"%s=%s\n", name_gen, executed_str);
138
      *ex_gen=1;
10 reyssat 139
    }
140
    else {
9093 bpr 141
      singlespace(vbuf_gen);
142
      fprintf(outf,"%s=!nosubst %s\n\n", name_gen, vbuf_gen);
10 reyssat 143
    }
144
}
145
 
9093 bpr 146
void p_hint(char *p[MAX_PARM]) {p_gen(&ex_hint, vbuf_hint, "hint", p);}
147
void p_help(char *p[MAX_PARM]) {p_gen(&ex_help, vbuf_help, "help", p);}
148
void p_solution(char *p[MAX_PARM]) {p_gen(&ex_solution, vbuf_solution, "solution", p);}
10 reyssat 149
 
2656 bpr 150
void p_latex(char *p[MAX_PARM])
151
{
152
    if(ex_latex<0) return;
153
    if(ex_latex>0 || p==NULL) {
7677 bpr 154
      out_exec(vbuf_latex,"latex");
155
      ex_latex=-1; return;
2656 bpr 156
    }
7677 bpr 157
    snprintf(vbuf_latex,sizeof(vbuf_latex),"%s",p[0]);
2656 bpr 158
    subst(vbuf_latex);
7677 bpr 159
      singlespace(vbuf_latex);
160
      fprintf(outf,"latex=!nosubst %s\n\n", vbuf_latex);
2656 bpr 161
}
162
 
10 reyssat 163
enum {typ_default, typ_num, typ_func, typ_units, typ_text,
164
      typ_formal,typ_matrix,typ_vector,typ_set,typ_equation,
165
      typ_case, typ_nocase, typ_atext, typ_wlist, typ_comp,
166
      typ_algexp, typ_litexp, typ_menu, typ_coord, typ_fill,
167
      typ_raw, typ_symtext,
168
      typ_java, typ_src, typ_chem
169
};
170
 
171
struct {
172
    char *name;
173
    int  type;
174
    char *def;
175
} anstype[]={
7677 bpr 176
      {"algexp", typ_algexp, "algexp"},
177
      {"aset",  typ_set, "aset"},
178
      {"atext",  typ_atext, "atext"},
179
      {"case",  typ_case, "case"},
180
      {"checkbox", typ_menu, "checkbox"},
10 reyssat 181
      {"chemeq",        typ_chem,       "chemeq"},
7677 bpr 182
      {"chset",  typ_atext, "chset"},
183
      {"click",  typ_menu, "click"},
184
      {"clickfill", typ_fill, "clickfill"},
185
      {"code",  typ_src, "code"},
186
      {"compose", typ_comp, "compose"},
187
      {"coord",  typ_coord, "coord"},
188
      {"coordinates", typ_coord, "coord"},
189
      {"corresp", typ_comp, "correspond"},
190
      {"correspond", typ_comp, "correspond"},
191
      {"default", typ_default, "default"},
192
      {"dragfill", typ_fill, "dragfill"},
193
      {"equation", typ_equation, "equation"},
194
      {"expalg", typ_algexp, "algexp"},
195
      {"formal", typ_formal, "formal"},
196
      {"fset",  typ_set, "fset"},
197
      {"function", typ_func, "function"},
198
      {"imgcomp", typ_comp, "imgcomp"},
199
      {"javacurve", typ_java, "javacurve"},
200
      {"link",  typ_menu, "click"},
201
      {"litexp", typ_litexp, "litexp"},
202
      {"mark",  typ_menu, "mark"},
203
      {"matrix", typ_matrix, "matrix"},
204
      {"menu",  typ_menu, "menu"},
205
      {"nocase", typ_nocase, "nocase"},
206
      {"number", typ_num, "numeric"},
207
      {"numeric", typ_num, "numeric"},
208
      {"numexp", typ_algexp, "numexp"},
209
      {"radio",  typ_menu, "radio"},
210
      {"range",  typ_func, "range"},
211
      {"ranges", typ_func, "range"},
212
      {"raw",  typ_raw, "raw"},
213
      {"reorder", typ_comp, "reorder"},
214
      {"select", typ_menu, "menu"},
215
      {"set",  typ_set, "set"},
216
      {"sigunits", typ_units, "sigunits"},
217
      {"symtext", typ_symtext, "symtext"},
218
      {"text",  typ_text, "case"},
219
      {"textcomp", typ_comp, "textcomp"},
220
      {"unit",  typ_units, "units"},
221
      {"units",  typ_units, "units"},
222
      {"vector", typ_vector, "vector"},
223
      {"wlist",  typ_wlist, "wlist"},
224
      {"wordcomp", typ_comp, "textcomp"}
10 reyssat 225
};
226
 
227
#define anstype_no (sizeof(anstype)/sizeof(anstype[0]))
228
 
229
void p_answer(char *p[MAX_PARM])
230
{
231
    char *pp, vbuf[MAX_LINELEN+1],nbuf[MAX_LINELEN+1];
232
    int i,j,k,typ;
7677 bpr 233
 
234
/* look for type definition */
10 reyssat 235
    typ=typ_default;
236
    for(i=0;i<5;i++) {
7677 bpr 237
      if(p[i]==NULL || p[i][0]==0) continue;
238
      p[i]=find_word_start(p[i]);
239
      if(strncasecmp(p[i],"type",strlen("type"))==0) {
240
          char *tt;
241
          tt=find_word_start(p[i]+strlen("type"));
242
          if(*tt=='=') {
243
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
244
            tt=find_word_start(tt+1); *find_word_end(tt)=0;
245
            k=search_list(anstype,anstype_no,sizeof(anstype[0]),tt);
246
/* unknown type is now substituted */
247
            if(k>=0) {
248
                fprintf(outf,"replytype%d=%s\n",
249
                      answercnt,anstype[k].def);
250
                typ=anstype[k].type;
251
            }
252
            else {
253
                snprintf(nbuf,sizeof(nbuf),"%s",tt); subst(nbuf);
254
                fprintf(outf,"replytype%d=%s\n\n",answercnt,nbuf);
255
            }
256
          }
257
          continue;
258
      }
259
      if(strncasecmp(p[i],"option",strlen("option"))==0) {
260
          char *tt, *tv;
261
          tt=p[i]+strlen("option");
262
          if(*tt=='s' || *tt=='S') tt++;
263
          tt=find_word_start(tt);
264
          if(*tt=='=') {
265
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
266
            snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
267
            for(tv=nbuf; *tv; tv++) if(*tv==',' || *tv==';') *tv=' ';
268
            strip_trailing_spaces(nbuf);
269
            fprintf(outf,"replyoption%d=%s \n",answercnt,
270
                  find_word_start(nbuf));
271
          }
272
          continue;
273
      }
274
      if(strncasecmp(p[i],"weight",strlen("weight"))==0) {
275
          char *tt;
276
          tt=p[i]+strlen("weight");
277
          tt=find_word_start(tt);
278
          if(*tt=='=') {
279
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
280
            snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
281
            strip_trailing_spaces(nbuf);
282
            fprintf(outf,"replyweight%d=%s \n",answercnt,
283
                  find_word_start(nbuf));
284
          }
285
          continue;
286
      }
10 reyssat 287
    }
288
    p[0]=find_word_start(p[0]);
289
    strncpy(nbuf,p[0],MAX_LINELEN); nbuf[MAX_LINELEN]=0; subst(nbuf);
290
    nbuf[MAX_PROMPTLEN]=0;
291
    strip_trailing_spaces(nbuf); pp=nbuf+strlen(nbuf)-1;
292
    if(*pp=='=') *pp=0;
293
    p[1]=find_word_start(p[1]);
294
    if(*p[1]=='\\' && (isalnum(*(p[1]+1)) || *(p[1]+1)=='_')) {
7677 bpr 295
/* check for analyzed answers */
296
      int i,n; char *pt;
297
      strncpy(vbuf,p[1]+1,MAX_LINELEN); vbuf[MAX_LINELEN]=0;
298
      pt=strchr(vbuf,';'); if(pt!=NULL) *pt=0;
299
      strip_trailing_spaces(vbuf); n=strlen(vbuf);
300
      if(n>=MAX_NAMELEN) goto normal;
301
      for(i=0;i<n && (isalnum(vbuf[i]) || vbuf[i]=='_');i++);
302
      if(i<n) goto normal;
303
      for(i=varcnt-1;i>=1 && strcmp(vbuf,param[i].name)!=0;i--);
304
      if(i<1) { /* unused name; the answer should be analyzed */
305
          char *pm;
306
          pm=xmalloc(MAX_NAMELEN+2);
307
          ovlstrcpy(pm,vbuf); param[varcnt].name=pm;
308
          if(pt) {
309
            *pt=';';
310
            ovlstrcpy(vbuf,pt); subst(vbuf); pt=vbuf;
311
          }
312
          else pt="";
313
          param[varcnt].type=pt_real;
314
          param[varcnt].save=1;
315
          fprintf(outf,"replyname%d=%s\nreplygood%d=?analyze %d%s\n",
316
                answercnt,nbuf,answercnt,varcnt,pt);
317
          condans++; answercnt++; varcnt++; return;
318
      }
10 reyssat 319
    }
320
    normal:
321
    strncpy(vbuf,p[1],MAX_LINELEN); vbuf[MAX_LINELEN]=0;
322
    subst(vbuf);
323
    switch(typ) {
7677 bpr 324
      default:
325
      case typ_default: {
326
          fprintf(outf,"replyname%d=%s\nreplygood%d=%s\n",
327
                answercnt,nbuf,answercnt,vbuf);
328
          break;
329
      }
330
      case typ_num: {
331
          fprintf(outf,"replyname%d=%s\nreplygood%d=$[%s]\n",
332
                answercnt,nbuf,answercnt,vbuf);
333
          break;
334
      }
335
      case typ_equation:
336
      case typ_func: {
337
          fprintf(outf,"replyname%d=%s\nreplygood%d=!rawmath %s\n",
338
                answercnt,nbuf,answercnt,vbuf);
339
          break;
340
      }
341
      case typ_units: {
342
          fprintf(outf,"replyname%d=%s\nreplygood%d=%s\n",
343
                answercnt,nbuf,answercnt,vbuf);
344
          break;
345
      }
10 reyssat 346
    }
347
    answercnt++;
348
}
349
 
350
void p_choice(char *p[MAX_PARM])
351
{
352
    int i,j;
353
    char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1],nbuf[MAX_LINELEN+1];
354
    for(i=0;i<5;i++) {
7677 bpr 355
      if(p[i]==NULL || p[i][0]==0) continue;
356
      p[i]=find_word_start(p[i]);
357
      if(strncasecmp(p[i],"option",strlen("option"))==0) {
358
          char *tt, *tv;
359
          tt=p[i]+strlen("option");
360
          if(*tt=='s' || *tt=='S') tt++;
361
          tt=find_word_start(tt);
362
          if(*tt=='=') {
363
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
364
            snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
365
            for(tv=nbuf; *tv; tv++) if(*tv==',' || *tv==';') *tv=' ';
366
            strip_trailing_spaces(nbuf);
367
            fprintf(outf,"choiceoption%d=%s \n",choicecnt,
368
                  find_word_start(nbuf));
369
          }
370
          continue;
371
      }
372
      if(strncasecmp(p[i],"weight",strlen("weight"))==0) {
373
          char *tt;
374
          tt=p[i]+strlen("weight");
375
          tt=find_word_start(tt);
376
          if(*tt=='=') {
377
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
378
            snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
379
            strip_trailing_spaces(nbuf);
380
            fprintf(outf,"choiceweight%d=%s \n",choicecnt,
381
                  find_word_start(nbuf));
382
          }
383
          continue;
384
      }
10 reyssat 385
    }
386
    p[0]=find_word_start(p[0]);
387
    snprintf(buf1,sizeof(buf1),"%s",p[1]); subst(buf1);
388
    snprintf(buf2,sizeof(buf2),"%s",p[2]); subst(buf2);
389
    snprintf(nbuf,sizeof(nbuf),"%s",p[0]); subst(nbuf);
390
    nbuf[MAX_PROMPTLEN]=0;
391
    fprintf(outf,"choicename%d=%s\nchoicegood%d=%s\nchoicebad%d=%s\n",
7677 bpr 392
          choicecnt,nbuf,choicecnt,buf1,choicecnt,buf2);
10 reyssat 393
    choicecnt++;
394
}
395
 
396
void putval(char *p, int n, int ptype)
397
{
398
    switch(ptype) {
7677 bpr 399
      case pt_int: {
400
          fprintf(outf,"val%d=$[rint(%s)]\n",n,p);
401
          break;
402
      }
403
      case pt_real: {
404
          fprintf(outf,"val%d=$[%s]\n",n,p);
405
          break;
406
      }
407
      case pt_func: {
408
          fprintf(outf,"val%d=!rawmath %s\n",n,p);
409
          break;
410
      }
411
      case pt_complex: {
412
          fprintf(outf,"t_=!rawmath %s\nt_=!exec pari print($t_)\n\
10 reyssat 413
val%d=!mathsubst I=i in $t_\n",p,n);
7677 bpr 414
          break;
415
      }
416
      case pt_matrix: {
417
          fprintf(outf,"tmp=!trim %s\n\
7688 bpr 418
val%d=!translate internal $     \\\n$ to ;; in $tmp\n",p,n);
7677 bpr 419
          break;
420
      }
421
      case pt_rat: {
422
          fprintf(outf,"t_=!rawmath %s\n\
10 reyssat 423
val%d=!exec pari print($t_)\n",p,n);
7677 bpr 424
          break;
425
      }
426
      default: {
427
          fprintf(outf,"val%d=%s\n",n,p);
428
          break;
429
      }
10 reyssat 430
    }
431
}
432
 
433
void parm(char *p[MAX_PARM], int ptype)
434
{
435
    char *pp, *p2;
436
    char vbuf[MAX_LINELEN+1];
437
    int i;
438
 
439
    p[0]=find_word_start(p[0]);
440
    if(*p[0]=='\\') p[0]++;
7677 bpr 441
/* bad name */
10 reyssat 442
    if(!isalpha(*p[0])) return;
443
    strip_trailing_spaces(p[0]);
444
    for(pp=p[0];*pp;pp++) if(!isalnum(*pp) && *pp!='_') {
7677 bpr 445
/* bad name and security risk */
446
      if(!isspace(*pp)) return;
447
      ovlstrcpy(pp,pp+1); pp--;
10 reyssat 448
    }
449
    for(i=1;i<varcnt && strcmp(p[0],param[i].name)!=0;i++);
450
    p[1]=find_word_start(p[1]);
451
    snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf);
452
    if(deftag) repsubst(vbuf);
453
    if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?') {
7677 bpr 454
      char buf[MAX_LINELEN+1];
455
      if(check_compare(vbuf)==0) goto noif;
456
      p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
457
      snprintf(buf,sizeof(buf),"%s",vbuf);
458
      prepcnt=0; parmprep(buf,ptype);
459
      fprintf(outf,"\n!ifval %s\n",buf);
460
      snprintf(buf,sizeof(buf),"%s",pp);
461
      parmprep(buf,ptype); putval(buf,i,ptype);
462
      if(p2!=NULL) {
463
          fprintf(outf,"!else\n");
464
          snprintf(buf,sizeof(buf),"%s",p2);
465
          parmprep(buf,ptype); putval(buf,i,ptype);
466
      }
467
      fprintf(outf,"!endif\n");
10 reyssat 468
    }
469
    else {
470
noif:
7677 bpr 471
      prepcnt=0; parmprep(vbuf, ptype);
472
      putval(vbuf,i,ptype);
10 reyssat 473
    }
474
    if(i>=varcnt && i<MAX_PARAM) {
7677 bpr 475
      param[varcnt].name=p[0];
476
      param[varcnt].type=ptype;
477
      param[varcnt].save=0;
478
      varcnt++;
10 reyssat 479
    }
480
}
481
 
482
void p_int(char *p[MAX_PARM]) {parm(p,pt_int);}
483
void p_rational(char *p[MAX_PARM]) {parm(p,pt_rat);}
484
void p_real(char *p[MAX_PARM]) {parm(p,pt_real);}
485
void p_complex(char *p[MAX_PARM]) {parm(p,pt_complex);}
486
void p_func(char *p[MAX_PARM]) {parm(p,pt_func);}
487
void p_text(char *p[MAX_PARM]) {parm(p,pt_text);}
488
void p_matrix(char *p[MAX_PARM]) {parm(p,pt_matrix);}
489
 
490
void p_parm(char *p[MAX_PARM])
491
{
492
    parm(p,pt_real);
493
}
494
 
8367 bpr 495
void _p_if(char *p[MAX_PARM], int type)
10 reyssat 496
{
497
    char vbuf[MAX_LINELEN+1];
498
    snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf);
499
    if(deftag) repsubst(vbuf);
500
    prepcnt=0; parmprep(vbuf, pt_real);
8367 bpr 501
    switch(type) {
502
          case 0: fprintf(outf,"!if %s \n",vbuf); break;
503
          case 1: fprintf(outf,"!ifval %s \n",vbuf);
504
    }
10 reyssat 505
}
8367 bpr 506
void p_if(char *p[MAX_PARM]) { return _p_if(p, 0);}
507
void p_ifval(char *p[MAX_PARM]) { return _p_if(p, 1);}
10 reyssat 508
 
509
void p_else(char *p[MAX_PARM])
510
{
511
    fprintf(outf,"!else\n");
512
}
513
 
514
void p_endif(char *p[MAX_PARM])
515
{
516
    fprintf(outf,"!endif\n");
517
}
518
 
519
 
520
void p_while(char *p[MAX_PARM])
521
{
522
    char vbuf[MAX_LINELEN+1];
523
    snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf);
524
    if(deftag) repsubst(vbuf);
525
    prepcnt=0; parmprep(vbuf, pt_real);
526
    fprintf(outf,"!while %s \n",vbuf);
527
}
528
 
529
void p_endwhile(char *p[MAX_PARM])
530
{
531
    fprintf(outf,"!endwhile\n");
532
}
533
 
534
void p_for(char *p[MAX_PARM])
535
{
536
    char *p1, *p2, buf[256];
537
    char vbuf[MAX_LINELEN+1];
538
    int i;
539
 
540
    p1=find_word_start(p[0]);
541
    if(!isalpha(*p1)) return;
542
    for(p2=p1; isalnum(*p2); p2++);
543
    if(p2-p1>64) return;
544
    memmove(buf,p1,p2-p1); buf[p2-p1]=0;
545
    for(i=1;i<varcnt && strcmp(buf,param[i].name)!=0;i++);
546
    if(i>=varcnt && i<MAX_PARAM) {
7677 bpr 547
      param[varcnt].name=p1;
548
      param[varcnt].type=pt_real;
549
      param[varcnt].save=0;
550
      varcnt++;
10 reyssat 551
    }
552
    snprintf(vbuf,sizeof(vbuf),"%s",p2); subst(vbuf); *p2=0;
553
    if(deftag) repsubst(vbuf);
554
    prepcnt=0; parmprep(vbuf, pt_real);
555
    fprintf(outf,"!for val%d %s \n", i, vbuf);
556
}
557
 
558
void p_next(char *p[MAX_PARM])
559
{
560
    fprintf(outf,"!next\n");
561
}
562
 
563
void p_plot(char *p[MAX_PARM])
564
{
565
    int i, f, xr, yr;
566
    char *pp, *p2;
567
    char buf[MAX_LINELEN+1];
7677 bpr 568
    f=xr=yr=-1;
10 reyssat 569
    for(i=0;i<3;i++) {
7677 bpr 570
      if(*p[i]==0) continue;
571
      if((pp=strchr(p[i],'='))==NULL) f=i;
572
      else {
573
          *pp=0; pp++;
574
          p2=find_word_start(p[i]);
575
          if(*p2=='x' || *p2=='X') xr=i;
576
          else if (*p2=='y' || *p2=='Y') yr=i;
577
          ovlstrcpy(p[i],pp);
578
      }
10 reyssat 579
    }
580
/*    if(xr>=0 && (pp=strstr(p[xr],".."))!=NULL) {
7677 bpr 581
 
10 reyssat 582
    }
583
*/    if(f<0) return;
3718 reyssat 584
    ovlstrcpy(buf, p[f]);
10 reyssat 585
    prepcnt=0; parmprep(buf,pt_func);
586
    fprintf(outf,"plot_fn=!rawmath %s\n",buf);
7677 bpr 587
 
10 reyssat 588
}
589
 
590
void p_condition(char *p[MAX_PARM])
591
{
592
    int i,j;
593
    char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1];
594
    for(i=0;i<5;i++) {
7677 bpr 595
      if(p[i]==NULL || p[i][0]==0) continue;
596
      p[i]=find_word_start(p[i]);
597
      if(strncasecmp(p[i],"option",strlen("option"))==0) {
598
          char *tt, *tv;
599
          tt=p[i]+strlen("option");
600
          if(*tt=='s' || *tt=='S') tt++;
601
          tt=find_word_start(tt);
602
          if(*tt=='=') {
603
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
604
            snprintf(buf1,sizeof(buf1),"%s",tt+1); subst(buf1);
605
            for(tv=buf1; *tv; tv++) if(*tv==',' || *tv==';') *tv=' ';
606
            strip_trailing_spaces(buf1);
607
            fprintf(outf,"condoption%d=%s \n",conditioncnt,
608
                  find_word_start(buf1));
609
          }
610
          continue;
611
      }
612
      if(strncasecmp(p[i],"weight",strlen("weight"))==0) {
613
          char *tt;
614
          tt=p[i]+strlen("weight");
615
          tt=find_word_start(tt);
616
          if(*tt=='=') {
617
            for(j=i;j<6;j++) p[j]=p[j+1]; i--;
618
            snprintf(buf1,sizeof(buf1),"%s",tt+1); subst(buf1);
619
            strip_trailing_spaces(buf1);
620
            fprintf(outf,"condweight%d=%s \n",conditioncnt,
621
                  find_word_start(buf1));
622
          }
623
          continue;
624
      }
10 reyssat 625
    }
626
    if(p[1][0]==0) {p[1]=p[0]; p[0]="";}
627
    snprintf(buf1,sizeof(buf1),"%s",p[0]); subst(buf1);
628
    snprintf(buf2,sizeof(buf2),"%s",p[1]); subst(buf2);
629
    prepcnt=0; parmprep(buf2, pt_real);
630
    repsubst(buf2);
631
    fprintf(outf,"\n!ifval %s\n condtest%d=1\n!else\n condtest%d=0\n!endif\n\
632
condname%d=%s\n", buf2,conditioncnt,conditioncnt,conditioncnt,buf1);
633
    conditioncnt++;
634
}
635
 
636
void p_conditions(char *p[MAX_PARM])
637
{
638
    char buf[MAX_LINELEN+1];
639
    snprintf(buf,sizeof(buf),"%s",p[0]); subst(buf);
640
    prepcnt=0; parmprep(buf, pt_real);
641
    repsubst(buf);
642
    fprintf(outf,"\ncondlist=%s\n",buf);
643
}
644
 
645
void p_feedback(char *p[MAX_PARM])
646
{
647
    char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1];
648
    char *cmpstr="ifval";
7677 bpr 649
 
10 reyssat 650
    snprintf(buf1,sizeof(buf1),"%s",p[0]); subst(buf1);
651
    snprintf(buf2,sizeof(buf2),"%s",p[1]); subst(buf2);
652
    repsubst(buf1); repsubst(buf2);
653
    if(strstr(buf1,"$m_choice")!=NULL) cmpstr="if";
654
    prepcnt=0; setpre="!set "; parmprep(buf1, pt_real); setpre="";
6450 obado 655
    fprintf(outf,"!%s %s\n <div class='oef_feedbacks'>",cmpstr, buf1);
10 reyssat 656
    out_exec(buf2,NULL);
6450 obado 657
    fprintf(outf,"</div>\n!endif\n");
10 reyssat 658
}
659
 
7677 bpr 660
/* definition of steps */
10 reyssat 661
void p_steps(char *p[MAX_PARM])
662
{
663
    char vbuf[MAX_LINELEN+1];
664
    char *pp, *p2;
7677 bpr 665
 
10 reyssat 666
    snprintf(vbuf,sizeof(vbuf),"%s",find_word_start(p[0])); subst(vbuf);
667
    strip_trailing_spaces(vbuf);
668
    if(vbuf[0]==0) return;
669
    if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?') {
7677 bpr 670
      char buf[MAX_LINELEN+1];
671
      if(check_compare(vbuf)==0) goto noif;
672
      p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
673
      snprintf(buf,sizeof(buf),"%s",vbuf);
674
      prepcnt=0; parmprep(buf,pt_text);
675
      fprintf(outf,"\n!ifval %s \n",buf);
676
      snprintf(buf,sizeof(buf),"%s",pp);
677
      parmprep(buf,pt_text);
678
      fprintf(outf,"oefsteps=%s \n",buf);
679
      if(p2!=NULL) {
680
          fprintf(outf,"!else\n");
681
          snprintf(buf,sizeof(buf),"%s",p2);
682
          parmprep(buf,pt_text);
683
          fprintf(outf,"oefsteps=%s \n",buf);
684
      }
685
      fprintf(outf,"!endif\n");
10 reyssat 686
    }
687
    else {
688
noif:
7677 bpr 689
      prepcnt=0; parmprep(vbuf, pt_text);
690
      fprintf(outf,"oefsteps=%s \nnextstep=!nosubst %s \n",vbuf,vbuf);
10 reyssat 691
    }
692
    fprintf(outf,"!readproc oef/steps.proc\n");
693
}
694
 
7677 bpr 695
/* dynamic steps */
10 reyssat 696
void p_nextstep(char *p[MAX_PARM])
697
{
698
    fprintf(outf,"dynsteps=yes\n");
699
    p_steps(p);
700
}
701