Subversion Repositories wimsdev

Rev

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