Subversion Repositories wimsdev

Rev

Rev 11123 | Rev 12122 | 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. #include "../Lib/libwims.h"
  18. #include "oef2wims.h"
  19. int prepcnt;
  20. int ex_statement=0, ex_hint=0, ex_help=0, ex_solution=0, ex_latex=0;
  21.  
  22. const size_t MAX_KEY_LEN=128;
  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];
  27. char vbuf_latex[MAX_LINELEN+1];
  28.  
  29. /* empty processor, template. */
  30. void empty(char *p[MAX_PARM]) {}
  31.  
  32. void p_header(char *p[MAX_PARM])
  33. {
  34.     p[0]=find_word_start(p[0]);
  35.     if(strlen(p[0])>MAX_KEY_LEN) p[0][MAX_KEY_LEN]=0;
  36.     fprintf(outf,"%s=%s\n", p[1], p[0]);
  37. }
  38.  
  39. void p_computeanswer(char *p[MAX_PARM])
  40. {
  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. }
  55. void p_gen2(char *name_gen, char *p[MAX_PARM])
  56. {
  57.     char vbuf_gen[MAX_LINELEN+1];
  58.     if(p==NULL) return;
  59.     snprintf(vbuf_gen,sizeof(vbuf_gen),"%s",p[0]); subst(vbuf_gen);
  60.     singlespace(vbuf_gen);
  61.     fprintf(outf,"%s=%s\n", name_gen, vbuf_gen);
  62. }
  63. void p_css(char *p[MAX_PARM]) {p_gen2("oefcss", p);}
  64. void p_credits(char *p[MAX_PARM]){p_gen2("credits", p);}
  65. void p_description(char *p[MAX_PARM]){p_gen2("oefdescription", p);}
  66. void p_observation(char *p[MAX_PARM]){p_gen2("oefobservation", p);}
  67.  
  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);
  72. /* the second condition could be removed?
  73.  * To be very careful! */
  74.     if(strchr(vbuf,'=')==NULL || strchr(vbuf,'!')!=NULL) return;
  75.     fprintf(outf,"%s\n",vbuf);
  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);
  83. /* the second condition could be removed?
  84.  * To be very careful! */
  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])
  102. {
  103.     p[0]=find_word_start(p[0]);
  104.     *find_word_end(p[0])=0;
  105.     if(strlen(p[0])==2) fprintf(outf,"language=%s\n",p[0]);
  106. }
  107.  
  108. void p_statement(char *p[MAX_PARM])
  109. {
  110.     if(ex_statement<0) return;
  111.     if(ex_statement>0 || p==NULL) {
  112.       out_exec(vbuf_statement,"question");
  113.       ex_statement=-1; return;
  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) {
  119.       fprintf(outf,"question=!nosubst %s\n",vbuf_statement);
  120.       ex_statement=-1;
  121.     }
  122.     else {
  123.       fprintf(outf,"question=%s\n",executed_str);
  124.       ex_statement=1;
  125.     }
  126. }
  127.  
  128. void p_gen(int *ex_gen, char vbuf_gen[], char *name_gen, char *p[MAX_PARM])
  129. {
  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;
  134.     }
  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;
  139.     }
  140.     else {
  141.       singlespace(vbuf_gen);
  142.       fprintf(outf,"%s=!nosubst %s\n\n", name_gen, vbuf_gen);
  143.     }
  144. }
  145.  
  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);}
  149.  
  150. void p_latex(char *p[MAX_PARM])
  151. {
  152.     if(ex_latex<0) return;
  153.     if(ex_latex>0 || p==NULL) {
  154.       out_exec(vbuf_latex,"latex");
  155.       ex_latex=-1; return;
  156.     }
  157.     snprintf(vbuf_latex,sizeof(vbuf_latex),"%s",p[0]);
  158.     subst(vbuf_latex);
  159.       singlespace(vbuf_latex);
  160.       fprintf(outf,"latex=!nosubst %s\n\n", vbuf_latex);
  161. }
  162.  
  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[]={
  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"},
  181.       {"chemeq",        typ_chem,       "chemeq"},
  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"}
  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;
  233.  
  234. /* look for type definition */
  235.     typ=typ_default;
  236.     for(i=0;i<5;i++) {
  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];
  244.             i--;
  245.             tt=find_word_start(tt+1); *find_word_end(tt)=0;
  246.             k=search_list(anstype,anstype_no,sizeof(anstype[0]),tt);
  247. /* unknown type is now substituted */
  248.             if(k>=0) {
  249.                 fprintf(outf,"replytype%d=%s\n",
  250.                       answercnt,anstype[k].def);
  251.                 typ=anstype[k].type;
  252.             }
  253.             else {
  254.                 snprintf(nbuf,sizeof(nbuf),"%s",tt); subst(nbuf);
  255.                 fprintf(outf,"replytype%d=%s\n\n",answercnt,nbuf);
  256.             }
  257.           }
  258.           continue;
  259.       }
  260.       if(strncasecmp(p[i],"option",strlen("option"))==0) {
  261.           char *tt, *tv;
  262.           tt=p[i]+strlen("option");
  263.           if(*tt=='s' || *tt=='S') tt++;
  264.           tt=find_word_start(tt);
  265.           if(*tt=='=') {
  266.             for(j=i;j<6;j++) p[j]=p[j+1];
  267.             i--;
  268.             snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
  269.             for(tv=nbuf; *tv; tv++) if(*tv==',' || *tv==';') *tv=' ';
  270.             strip_trailing_spaces(nbuf);
  271.             fprintf(outf,"replyoption%d=%s \n",answercnt,
  272.                   find_word_start(nbuf));
  273.           }
  274.           continue;
  275.       }
  276.       if(strncasecmp(p[i],"weight",strlen("weight"))==0) {
  277.           char *tt;
  278.           tt=p[i]+strlen("weight");
  279.           tt=find_word_start(tt);
  280.           if(*tt=='=') {
  281.             for(j=i;j<6;j++) p[j]=p[j+1];
  282.             i--;
  283.             snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
  284.             strip_trailing_spaces(nbuf);
  285.             fprintf(outf,"replyweight%d=%s \n",answercnt,
  286.                   find_word_start(nbuf));
  287.           }
  288.           continue;
  289.       }
  290.     }
  291.     p[0]=find_word_start(p[0]);
  292.     strncpy(nbuf,p[0],MAX_LINELEN); nbuf[MAX_LINELEN]=0; subst(nbuf);
  293.     nbuf[MAX_PROMPTLEN]=0;
  294.     strip_trailing_spaces(nbuf); pp=nbuf+strlen(nbuf)-1;
  295.     if(*pp=='=') *pp=0;
  296.     p[1]=find_word_start(p[1]);
  297.     if(*p[1]=='\\' && (isalnum(*(p[1]+1)) || *(p[1]+1)=='_')) {
  298. /* check for analyzed answers */
  299.       int i,n; char *pt;
  300.       strncpy(vbuf,p[1]+1,MAX_LINELEN); vbuf[MAX_LINELEN]=0;
  301.       pt=strchr(vbuf,';'); if(pt!=NULL) *pt=0;
  302.       strip_trailing_spaces(vbuf); n=strlen(vbuf);
  303.       if(n>=MAX_NAMELEN) goto normal;
  304.       for(i=0;i<n && (isalnum(vbuf[i]) || vbuf[i]=='_');i++);
  305.       if(i<n) goto normal;
  306.       for(i=varcnt-1;i>=1 && strcmp(vbuf,param[i].name)!=0;i--);
  307.       if(i<1) { /* unused name; the answer should be analyzed */
  308.           char *pm;
  309.           pm=xmalloc(MAX_NAMELEN+2);
  310.           ovlstrcpy(pm,vbuf); param[varcnt].name=pm;
  311.           if(pt) {
  312.             *pt=';';
  313.             ovlstrcpy(vbuf,pt); subst(vbuf); pt=vbuf;
  314.           }
  315.           else pt="";
  316.           param[varcnt].type=pt_real;
  317.           param[varcnt].save=1;
  318.           fprintf(outf,"replyname%d=%s\nreplygood%d=?analyze %d%s\n",
  319.                 answercnt,nbuf,answercnt,varcnt,pt);
  320.           condans++; answercnt++; varcnt++; return;
  321.       }
  322.     }
  323.     normal:
  324.     strncpy(vbuf,p[1],MAX_LINELEN); vbuf[MAX_LINELEN]=0;
  325.     subst(vbuf);
  326.     switch(typ) {
  327.       default:
  328.       case typ_default: {
  329.           fprintf(outf,"replyname%d=%s\nreplygood%d=%s\n",
  330.                 answercnt,nbuf,answercnt,vbuf);
  331.           break;
  332.       }
  333.       case typ_num: {
  334.           fprintf(outf,"replyname%d=%s\nreplygood%d=$[%s]\n",
  335.                 answercnt,nbuf,answercnt,vbuf);
  336.           break;
  337.       }
  338.       case typ_equation:
  339.       case typ_func: {
  340.           fprintf(outf,"replyname%d=%s\nreplygood%d=!rawmath %s\n",
  341.                 answercnt,nbuf,answercnt,vbuf);
  342.           break;
  343.       }
  344.       case typ_units: {
  345.           fprintf(outf,"replyname%d=%s\nreplygood%d=%s\n",
  346.                 answercnt,nbuf,answercnt,vbuf);
  347.           break;
  348.       }
  349.     }
  350.     answercnt++;
  351. }
  352.  
  353. void p_choice(char *p[MAX_PARM])
  354. {
  355.     int i,j;
  356.     char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1],nbuf[MAX_LINELEN+1];
  357.     for(i=0;i<5;i++) {
  358.       if(p[i]==NULL || p[i][0]==0) continue;
  359.       p[i]=find_word_start(p[i]);
  360.       if(strncasecmp(p[i],"option",strlen("option"))==0) {
  361.           char *tt, *tv;
  362.           tt=p[i]+strlen("option");
  363.           if(*tt=='s' || *tt=='S') tt++;
  364.           tt=find_word_start(tt);
  365.           if(*tt=='=') {
  366.             for(j=i;j<6;j++) p[j]=p[j+1];
  367.             i--;
  368.             snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
  369.             for(tv=nbuf; *tv; tv++) if(*tv==',' || *tv==';') *tv=' ';
  370.             strip_trailing_spaces(nbuf);
  371.             fprintf(outf,"choiceoption%d=%s \n",choicecnt,
  372.                   find_word_start(nbuf));
  373.           }
  374.           continue;
  375.       }
  376.       if(strncasecmp(p[i],"weight",strlen("weight"))==0) {
  377.           char *tt;
  378.           tt=p[i]+strlen("weight");
  379.           tt=find_word_start(tt);
  380.           if(*tt=='=') {
  381.             for(j=i;j<6;j++) p[j]=p[j+1];
  382.             i--;
  383.             snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf);
  384.             strip_trailing_spaces(nbuf);
  385.             fprintf(outf,"choiceweight%d=%s \n",choicecnt,
  386.                   find_word_start(nbuf));
  387.           }
  388.           continue;
  389.       }
  390.     }
  391.     p[0]=find_word_start(p[0]);
  392.     snprintf(buf1,sizeof(buf1),"%s",p[1]); subst(buf1);
  393.     snprintf(buf2,sizeof(buf2),"%s",p[2]); subst(buf2);
  394.     snprintf(nbuf,sizeof(nbuf),"%s",p[0]); subst(nbuf);
  395.     nbuf[MAX_PROMPTLEN]=0;
  396.     fprintf(outf,"choicename%d=%s\nchoicegood%d=%s\nchoicebad%d=%s\n",
  397.           choicecnt,nbuf,choicecnt,buf1,choicecnt,buf2);
  398.     choicecnt++;
  399. }
  400.  
  401. void putval(char *p, int n, int ptype)
  402. {
  403.     switch(ptype) {
  404.       case pt_int: {
  405.           fprintf(outf,"val%d=$[rint(%s)]\n",n,p);
  406.           break;
  407.       }
  408.       case pt_real: {
  409.           fprintf(outf,"val%d=$[%s]\n",n,p);
  410.           break;
  411.       }
  412.       case pt_func: {
  413.           fprintf(outf,"val%d=!rawmath %s\n",n,p);
  414.           break;
  415.       }
  416.       case pt_complex: {
  417.           fprintf(outf,"t_=!rawmath %s\nt_=!exec pari print($t_)\n\
  418. val%d=!mathsubst I=i in $t_\n",p,n);
  419.           break;
  420.       }
  421.       case pt_matrix: {
  422.           fprintf(outf,"tmp=!trim %s\n\
  423. val%d=!translate internal $     \\\n$ to ;; in $tmp\n",p,n);
  424.           break;
  425.       }
  426.       case pt_rat: {
  427.           fprintf(outf,"t_=!rawmath %s\n\
  428. val%d=!exec pari print($t_)\n",p,n);
  429.           break;
  430.       }
  431.       default: {
  432.           fprintf(outf,"val%d=%s\n",n,p);
  433.           break;
  434.       }
  435.     }
  436. }
  437.  
  438. void parm(char *p[MAX_PARM], int ptype)
  439. {
  440.     char *pp, *p2;
  441.     char vbuf[MAX_LINELEN+1];
  442.     int i;
  443.  
  444.     p[0]=find_word_start(p[0]);
  445.     if(*p[0]=='\\') p[0]++;
  446. /* bad name */
  447.     if(!isalpha(*p[0])) return;
  448.     strip_trailing_spaces(p[0]);
  449.     for(pp=p[0];*pp;pp++) if(!isalnum(*pp) && *pp!='_') {
  450. /* bad name and security risk */
  451.       if(!isspace(*pp)) return;
  452.       ovlstrcpy(pp,pp+1); pp--;
  453.     }
  454.     for(i=1;i<varcnt && strcmp(p[0],param[i].name)!=0;i++);
  455.     p[1]=find_word_start(p[1]);
  456.     snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf);
  457.     if(deftag) repsubst(vbuf);
  458.     if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?') {
  459.       char buf[MAX_LINELEN+1];
  460.       if(check_compare(vbuf)==0) goto noif;
  461.       p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
  462.       snprintf(buf,sizeof(buf),"%s",vbuf);
  463.       prepcnt=0; parmprep(buf,ptype);
  464.       fprintf(outf,"\n!ifval %s\n",buf);
  465.       snprintf(buf,sizeof(buf),"%s",pp);
  466.       parmprep(buf,ptype); putval(buf,i,ptype);
  467.       if(p2!=NULL) {
  468.           fprintf(outf,"!else\n");
  469.           snprintf(buf,sizeof(buf),"%s",p2);
  470.           parmprep(buf,ptype); putval(buf,i,ptype);
  471.       }
  472.       fprintf(outf,"!endif\n");
  473.     }
  474.     else {
  475. noif:
  476.       prepcnt=0; parmprep(vbuf, ptype);
  477.       putval(vbuf,i,ptype);
  478.     }
  479.     if(i>=varcnt && i<MAX_PARAM) {
  480.       param[varcnt].name=p[0];
  481.       param[varcnt].type=ptype;
  482.       param[varcnt].save=0;
  483.       varcnt++;
  484.     }
  485. }
  486.  
  487. void p_int(char *p[MAX_PARM]) {parm(p,pt_int);}
  488. void p_rational(char *p[MAX_PARM]) {parm(p,pt_rat);}
  489. void p_real(char *p[MAX_PARM]) {parm(p,pt_real);}
  490. void p_complex(char *p[MAX_PARM]) {parm(p,pt_complex);}
  491. void p_func(char *p[MAX_PARM]) {parm(p,pt_func);}
  492. void p_text(char *p[MAX_PARM]) {parm(p,pt_text);}
  493. void p_matrix(char *p[MAX_PARM]) {parm(p,pt_matrix);}
  494.  
  495. void p_parm(char *p[MAX_PARM])
  496. {
  497.     parm(p,pt_real);
  498. }
  499.  
  500. void _p_if(char *p[MAX_PARM], int type)
  501. {
  502.     char vbuf[MAX_LINELEN+1];
  503.     snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf);
  504.     if(deftag) repsubst(vbuf);
  505.     prepcnt=0; parmprep(vbuf, pt_real);
  506.     switch(type) {
  507.           case 0: fprintf(outf,"!if %s \n",vbuf); break;
  508.           case 1: fprintf(outf,"!ifval %s \n",vbuf);
  509.     }
  510. }
  511. void p_if(char *p[MAX_PARM]) { return _p_if(p, 0);}
  512. void p_ifval(char *p[MAX_PARM]) { return _p_if(p, 1);}
  513.  
  514. void p_else(char *p[MAX_PARM])
  515. {
  516.     fprintf(outf,"!else\n");
  517. }
  518.  
  519. void p_endif(char *p[MAX_PARM])
  520. {
  521.     fprintf(outf,"!endif\n");
  522. }
  523.  
  524.  
  525. void p_while(char *p[MAX_PARM])
  526. {
  527.     char vbuf[MAX_LINELEN+1];
  528.     snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf);
  529.     if(deftag) repsubst(vbuf);
  530.     prepcnt=0; parmprep(vbuf, pt_real);
  531.     fprintf(outf,"!while %s \n",vbuf);
  532. }
  533.  
  534. void p_endwhile(char *p[MAX_PARM])
  535. {
  536.     fprintf(outf,"!endwhile\n");
  537. }
  538.  
  539. void p_for(char *p[MAX_PARM])
  540. {
  541.     char *p1, *p2, buf[256];
  542.     char vbuf[MAX_LINELEN+1];
  543.     int i;
  544.  
  545.     p1=find_word_start(p[0]);
  546.     if(!isalpha(*p1)) return;
  547.     for(p2=p1; isalnum(*p2); p2++);
  548.     if(p2-p1>64) return;
  549.     memmove(buf,p1,p2-p1); buf[p2-p1]=0;
  550.     for(i=1;i<varcnt && strcmp(buf,param[i].name)!=0;i++);
  551.     if(i>=varcnt && i<MAX_PARAM) {
  552.       param[varcnt].name=p1;
  553.       param[varcnt].type=pt_real;
  554.       param[varcnt].save=0;
  555.       varcnt++;
  556.     }
  557.     snprintf(vbuf,sizeof(vbuf),"%s",p2); subst(vbuf); *p2=0;
  558.     if(deftag) repsubst(vbuf);
  559.     prepcnt=0; parmprep(vbuf, pt_real);
  560.     fprintf(outf,"!for val%d %s \n", i, vbuf);
  561. }
  562.  
  563. void p_next(char *p[MAX_PARM])
  564. {
  565.     fprintf(outf,"!next\n");
  566. }
  567.  
  568. void p_plot(char *p[MAX_PARM])
  569. {
  570.     int i, f, xr, yr;
  571.     char *pp, *p2;
  572.     char buf[MAX_LINELEN+1];
  573.     f=xr=yr=-1;
  574.     for(i=0;i<3;i++) {
  575.       if(*p[i]==0) continue;
  576.       if((pp=strchr(p[i],'='))==NULL) f=i;
  577.       else {
  578.           *pp=0; pp++;
  579.           p2=find_word_start(p[i]);
  580.           if(*p2=='x' || *p2=='X') xr=i;
  581.           else if (*p2=='y' || *p2=='Y') yr=i;
  582.           ovlstrcpy(p[i],pp);
  583.       }
  584.     }
  585. /*    if(xr>=0 && (pp=strstr(p[xr],".."))!=NULL) {
  586.  
  587.     }
  588. */    if(f<0) return;
  589.     ovlstrcpy(buf, p[f]);
  590.     prepcnt=0; parmprep(buf,pt_func);
  591.     fprintf(outf,"plot_fn=!rawmath %s\n",buf);
  592.  
  593. }
  594.  
  595. void p_condition(char *p[MAX_PARM])
  596. {
  597.     int i,j;
  598.     char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1];
  599.     for(i=0;i<5;i++) {
  600.       if(p[i]==NULL || p[i][0]==0) continue;
  601.       p[i]=find_word_start(p[i]);
  602.       if(strncasecmp(p[i],"option",strlen("option"))==0) {
  603.           char *tt, *tv;
  604.           tt=p[i]+strlen("option");
  605.           if(*tt=='s' || *tt=='S') tt++;
  606.           tt=find_word_start(tt);
  607.           if(*tt=='=') {
  608.             for(j=i;j<6;j++) p[j]=p[j+1];
  609.             i--;
  610.             snprintf(buf1,sizeof(buf1),"%s",tt+1); subst(buf1);
  611.             for(tv=buf1; *tv; tv++) if(*tv==',' || *tv==';') *tv=' ';
  612.             strip_trailing_spaces(buf1);
  613.             fprintf(outf,"condoption%d=%s \n",conditioncnt,
  614.                   find_word_start(buf1));
  615.           }
  616.           continue;
  617.       }
  618.       if(strncasecmp(p[i],"weight",strlen("weight"))==0) {
  619.           char *tt;
  620.           tt=p[i]+strlen("weight");
  621.           tt=find_word_start(tt);
  622.           if(*tt=='=') {
  623.             for(j=i;j<6;j++) p[j]=p[j+1];
  624.             i--;
  625.             snprintf(buf1,sizeof(buf1),"%s",tt+1); subst(buf1);
  626.             strip_trailing_spaces(buf1);
  627.             fprintf(outf,"condweight%d=%s \n",conditioncnt,
  628.                   find_word_start(buf1));
  629.           }
  630.           continue;
  631.       }
  632.     }
  633.     if(p[1][0]==0) {p[1]=p[0]; p[0]="";}
  634.     snprintf(buf1,sizeof(buf1),"%s",p[0]); subst(buf1);
  635.     snprintf(buf2,sizeof(buf2),"%s",p[1]); subst(buf2);
  636.     prepcnt=0; parmprep(buf2, pt_real);
  637.     repsubst(buf2);
  638.     fprintf(outf,"\n!ifval %s\n condtest%d=1\n!else\n condtest%d=0\n!endif\n\
  639. condname%d=%s\n", buf2,conditioncnt,conditioncnt,conditioncnt,buf1);
  640.     conditioncnt++;
  641. }
  642.  
  643. void p_conditions(char *p[MAX_PARM])
  644. {
  645.     char buf[MAX_LINELEN+1];
  646.     snprintf(buf,sizeof(buf),"%s",p[0]); subst(buf);
  647.     prepcnt=0; parmprep(buf, pt_real);
  648.     repsubst(buf);
  649.     fprintf(outf,"\ncondlist=%s\n",buf);
  650. }
  651.  
  652. void p_feedback(char *p[MAX_PARM])
  653. {
  654.     char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1];
  655.     char *cmpstr="ifval";
  656.  
  657.     snprintf(buf1,sizeof(buf1),"%s",p[0]); subst(buf1);
  658.     snprintf(buf2,sizeof(buf2),"%s",p[1]); subst(buf2);
  659.     repsubst(buf1); repsubst(buf2);
  660.     if(strstr(buf1,"$m_choice")!=NULL) cmpstr="if";
  661.     prepcnt=0; setpre="!set "; parmprep(buf1, pt_real); setpre="";
  662.     fprintf(outf,"!%s %s\n <div class='oef_feedbacks'>",cmpstr, buf1);
  663.     out_exec(buf2,NULL);
  664.     fprintf(outf,"</div>\n!endif\n");
  665. }
  666.  
  667. /* definition of steps */
  668. void p_steps(char *p[MAX_PARM])
  669. {
  670.     char vbuf[MAX_LINELEN+1];
  671.     char *pp, *p2;
  672.  
  673.     snprintf(vbuf,sizeof(vbuf),"%s",find_word_start(p[0])); subst(vbuf);
  674.     strip_trailing_spaces(vbuf);
  675.     if(vbuf[0]==0) return;
  676.     if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?') {
  677.       char buf[MAX_LINELEN+1];
  678.       if(check_compare(vbuf)==0) goto noif;
  679.       p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
  680.       snprintf(buf,sizeof(buf),"%s",vbuf);
  681.       prepcnt=0; parmprep(buf,pt_text);
  682.       fprintf(outf,"\n!ifval %s \n",buf);
  683.       snprintf(buf,sizeof(buf),"%s",pp);
  684.       parmprep(buf,pt_text);
  685.       fprintf(outf,"oefsteps=%s \n",buf);
  686.       if(p2!=NULL) {
  687.           fprintf(outf,"!else\n");
  688.           snprintf(buf,sizeof(buf),"%s",p2);
  689.           parmprep(buf,pt_text);
  690.           fprintf(outf,"oefsteps=%s \n",buf);
  691.       }
  692.       fprintf(outf,"!endif\n");
  693.     }
  694.     else {
  695. noif:
  696.       prepcnt=0; parmprep(vbuf, pt_text);
  697.       fprintf(outf,"oefsteps=%s \nnextstep=!nosubst %s \n",vbuf,vbuf);
  698.     }
  699.     fprintf(outf,"!readproc oef/steps.proc\n");
  700. }
  701.  
  702. /* dynamic steps */
  703. void p_nextstep(char *p[MAX_PARM])
  704. {
  705.     fprintf(outf,"dynsteps=yes\n");
  706.     p_steps(p);
  707. }
  708.  
  709.