Subversion Repositories wimsdev

Rev

Rev 11125 | Rev 11982 | 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. /* this is part of wims server source.
  19.  * This file does execution commands.
  20.  */
  21. #include "wims.h"
  22.  
  23. static void _skip_contents(int isif);
  24.  
  25. /* common routine for the two if's. */
  26. static void _exec_if_while(char *p, int numerical, int isif)
  27. {
  28.     if(compare(p,numerical,0)==0) _skip_contents(isif); /* skip if false */
  29.     else if(!isif) m_file.for_idx++;
  30.     return;
  31. }
  32.  
  33.     /* 'if' non-numerical (unless comparisons are < or >, etc.) */
  34. void exec_if(char *p)
  35. {
  36.     _exec_if_while(p,0,1);
  37. }
  38.  
  39. /* 'if' numerical. */
  40. void exec_ifval(char *p)
  41. {
  42.     _exec_if_while(p,1,1);
  43. }
  44.  
  45. void _exec_while(char *p, int numerical)
  46. {
  47.     FOR_STACK *stk;
  48.     if(m_file.for_idx>=MAX_FOR_LEVEL) module_error("too_many_fors");
  49.     stk=&(m_file.for_stack[m_file.for_idx]);
  50.     stk->lineno=m_file.l;
  51.     memmove(stk->varname,"?",2);
  52.     _exec_if_while(p,numerical,0);
  53. }
  54.  
  55. /* 'while' non-numerical (unless comparisons are < or >, etc.) */
  56. void exec_while(char *p)
  57. {
  58.     _exec_while(p,0);
  59. }
  60.  
  61. /* 'while' numerical. */
  62. void exec_whileval(char *p)
  63. {
  64.     _exec_while(p,1);
  65. }
  66.  
  67. void exec_endwhile(char *p)
  68. {
  69.     FOR_STACK *stk;
  70.     if(m_file.for_idx<=0) module_error("next_without_for");
  71.     stk=&(m_file.for_stack[m_file.for_idx-1]);
  72.     if(stk->varname[0]!='?') module_error("next_without_for");
  73.     m_file.for_idx--; *p=0;
  74.     m_file.linepointer=stk->lineno;
  75.     executed_gotos++;
  76.     if(executed_gotos>=GOTO_LIMIT) module_error("too_many_gotos");
  77. }
  78.  
  79. /* Should provide a method to stop infinite loop. */
  80. void exec_goto(char *p)
  81. {
  82.     char lbuf[MAX_NAMELEN+17];
  83.     char *label_start, *line_start;
  84.     int old_line;
  85.     int i;
  86.     executed_gotos++;
  87.     if(executed_gotos>=GOTO_LIMIT) module_error("too_many_gotos");
  88.     old_line=m_file.l;
  89.     label_start=find_word_start(p);
  90.     *find_word_end(label_start)=0;
  91.     m_file.l=m_file.linepointer=0;
  92.     for(i=0;i<m_file.linecnt;i++) {
  93.       if(((m_file.lines[i].isstart)&4)==0) continue;
  94.       line_start=find_word_start((m_file.lines[i]).address);
  95.       if(line_start>(m_file.lines[i+1]).address && i<m_file.linecnt)
  96.         continue;
  97.       m_file.linepointer=i;
  98.       wgetline(lbuf,MAX_NAMELEN+8,&m_file);
  99.       line_start=find_word_start(lbuf);
  100.       if(*line_start!=label_prefix_char) continue;
  101.       line_start++;*find_word_end(line_start)=0;
  102.       if(line_start[0]!='*' && strcmp(line_start,label_start)!=0) continue;
  103.       *p=0; i++; return;
  104.     }
  105.     m_file.l=old_line;
  106.     setvar(error_data_string,label_start); module_error("label_not_found");
  107. /*    m_file.linepointer=m_file.linecnt; *p=0;
  108.     return;
  109. */}
  110.  
  111. /* 'else' with or without 'if'.
  112.  * Philosophy: we implement a loosely checked grammar.
  113.  * We cannot check 'if' grammar if we want to use 'goto'
  114.  * together with 'if'.
  115.  */
  116. void exec_else(char *p)
  117. {
  118.     _skip_contents(1); return;
  119. }
  120.  
  121. /* 'endif': nothing needs to be done here. */
  122. void exec_endif(char *p)
  123. {
  124.     return;
  125. }
  126.  
  127. /* find out the end of a for loop */
  128. void goto_for_end(void)
  129. {
  130.     int inner;
  131.     char buf[16];
  132.     inner=0;
  133.     while(m_file.linepointer<m_file.linecnt) {
  134.       if((m_file.lines[m_file.linepointer].isstart&2)==0) {
  135.         m_file.linepointer++; continue;
  136.       }
  137.       if(wgetline(buf,8,&m_file)==EOF) break;
  138.       *find_word_end(buf)=0;
  139.       if(strcmp(buf,"!for")==0) {
  140.         inner++; continue;
  141.       }
  142.       if(strcmp(buf,"!next")==0) {
  143.         if(inner>0) {
  144.           inner--; continue;
  145.         }
  146.         else break;
  147.       }
  148.     }
  149. }
  150.  
  151. /* for */
  152. void exec_for(char *p)
  153. {
  154.     char *p1, *p2, *p3;
  155.     double v1, v2, step;
  156.     char buf[MAX_LINELEN+1];
  157.     FOR_STACK *stk;
  158.  
  159.     if(m_file.for_idx>=MAX_FOR_LEVEL) module_error("too_many_fors");
  160.     stk=&(m_file.for_stack[m_file.for_idx]);
  161.     stk->lineno=m_file.l;
  162.     p1=find_word_start(p);
  163.     for(p2=p1; !isspace(*p2) && *p2!=0 && *p2!='=' ; p2++);
  164.     if(*p2==0) syntax: module_error("for_syntax");
  165.     if(p2-p1>MAX_NAMELEN) module_error("name_too_long");
  166.     memmove(stk->varname,p1,p2-p1);
  167.     stk->varname[p2-p1]=0;
  168.     p1=find_word_start(p2); if(*p1==0) goto syntax;
  169.     if(*p1=='=') {
  170.       p1++;
  171.      assign:
  172.      stk->from=0;
  173.      p2=wordchr(p1,"to");
  174.      if(p2==NULL) p2=strstr(p1,"..");
  175.      if(p2==NULL) goto syntax;
  176.      *p2=0; p2+=2;
  177.      p3=wordchr(p2,"step");
  178.      if(p3!=NULL) {
  179.         *p3=0; p3+=strlen("step");
  180.         step=evalue(p3);
  181.         if(step==0) module_error("zero_step");
  182.      }
  183.      else step=1;
  184.      v1=evalue(p1); v2=evalue(p2);
  185.      float2str(v1,buf); setvar(stk->varname, buf);
  186.      if((step>0 && v1>v2) || (step<0 && v1<v2) ) {  /* condition not met */
  187.   loop_out:
  188.         goto_for_end();
  189.       }
  190.      else {
  191.         stk->varval=v1; stk->varend=v2; stk->step=step;
  192.         stk->lineno=m_file.linepointer;
  193.         m_file.for_idx++;
  194.      }
  195.      *p=0; return;
  196.     }
  197.     if(memcmp(p1,"from",strlen("from"))==0 && isspace(*(p1+strlen("from")))) {
  198.       p1+=strlen("from"); goto assign;
  199.     }
  200.     if(memcmp(p1,"in",strlen("in"))==0 && isspace(*(p1+strlen("in")))) {
  201.       stk->from=1; p1+=strlen("in");
  202.       p1=find_word_start(p1); if(*p1==0) goto loop_out;
  203.       p2=xmalloc(MAX_LINELEN+1);
  204.       mystrncpy(p2,p1,MAX_LINELEN);
  205.       substit(p2);
  206.       strip_trailing_spaces(p2);
  207.       if(*p2==0) goto loop_out;
  208.       p1=strparchr(p2,','); stk->bufpt=p2;
  209.       if(p1!=NULL) {
  210.         *p1=0; stk->list_pt=find_word_start(p1+1);
  211.       }
  212.       else stk->list_pt=NULL;
  213.       strip_trailing_spaces(p2); setvar(stk->varname,p2);
  214.       stk->lineno=m_file.linepointer;
  215.       m_file.for_idx++; *p=0; return;
  216.     }
  217.     goto syntax;
  218. }
  219.  
  220. /* break a for loop */
  221. void exec_break(char *p)
  222. {
  223.     FOR_STACK *stk;
  224.     if(m_file.for_idx>0) {
  225.       stk=&(m_file.for_stack[m_file.for_idx-1]);
  226.       if(stk->varname[0]=='?') _skip_contents(0);
  227.       else goto_for_end();
  228.       m_file.for_idx--;
  229.     }
  230.     *p=0; return;
  231. }
  232.  
  233. /* next */
  234. void exec_next(char *p)
  235. {
  236.     double v1, v2, step;
  237.     char *p1, *p2;
  238.     char buf[MAX_LINELEN+1];
  239.     FOR_STACK *stk;
  240.     if(m_file.for_idx<=0) module_error("next_without_for");
  241.     stk=&(m_file.for_stack[m_file.for_idx-1]);
  242.     if(stk->varname[0]=='?') module_error("next_without_for");
  243.     if(stk->from) { /* list style */
  244.       if(stk->list_pt==NULL) {
  245.         free(stk->bufpt);
  246.         m_file.for_idx--;
  247.         *p=0; return;
  248.       }
  249.       p1=strchr(stk->list_pt,',');
  250.       if(p1!=NULL) {
  251.         *p1=0; p2=find_word_start(p1+1);
  252.       }
  253.       else p2=NULL;
  254.       strip_trailing_spaces(stk->list_pt);
  255.       setvar(stk->varname,stk->list_pt);
  256.       stk->list_pt=p2; goto loop_back;
  257.     }
  258.     v1=stk->varval; v2=stk->varend; step=stk->step;
  259.     v1+=step; stk->varval=v1;
  260.     float2str(v1,buf);
  261.     setvar(stk->varname, buf);
  262.     if((step>0 && v1<=v2) || (step<0 && v1>=v2)) { /* loop */
  263.   loop_back:
  264.       m_file.linepointer=stk->lineno;
  265.       executed_gotos++;
  266.       if(executed_gotos>=GOTO_LIMIT) module_error("too_many_gotos");
  267.     }
  268.     else m_file.for_idx--;
  269.     *p=0; return;
  270. }
  271.  
  272. /* Execution of a file in the module directory,
  273.  * only for trusted modules.
  274.  */
  275. void exec_mexec(char *p)
  276. {
  277.     direct_exec=1;
  278.     calc_mexec(p);
  279.     direct_exec=0;
  280. }
  281.  
  282. /* call shell. */
  283. void _exec_ex(char *p, char *arg0, char *arg1, int n)
  284. {
  285.     char *abuf[8];
  286.     char errorfname[MAX_FNAME+1];
  287.  
  288.     if(robot_access) {
  289.       *p=0; return;
  290.     }
  291.     if(!noout || !trusted_module() || is_class_module) {
  292.       _calc_exec(p,arg0,arg1,n); if(outputing) output0(p);
  293.       return;
  294.     }
  295.     mkfname(errorfname,"%s/exec.err",tmp_dir);
  296.     abuf[0]=arg0; abuf[1]=arg1; abuf[n]=p; abuf[n+1]=NULL;
  297.     wrapexec=1; exportall();
  298.     execredirected(abuf[0],NULL,NULL,errorfname,abuf);
  299. }
  300.  
  301. /* call shell. */
  302. void exec_sh(char *p)
  303. {
  304.     _exec_ex(p,"sh","-c",2);
  305. }
  306.  
  307. /* call perl. */
  308. void exec_perl(char *p)
  309. {
  310.     _exec_ex(p,"perl","-e",2);
  311. }
  312.  
  313. /* file should not be cached */
  314. void exec_nocache(char *p)
  315. {    m_file.nocache|=1; *p=0; }
  316.  
  317. /*
  318.  * Read another file.
  319.  * Read a file in order to perform tests.
  320.  * Relevant global variables:
  321.  * - m_file, a WORKING_FILE structure (should be restored to its original
  322.  *   value at the end of the procedure)
  323.  * - untrust, an integer (should be restored to its original
  324.  *   value at the end of the procedure)
  325.  * - outputing, an integer; its value is juste read.
  326.  * - readnest, an integer, which is incremented during the function
  327.  *   and decremented when it finishes.
  328.  * Relevant global variables used in subprograms:
  329.  * - module_prefix, a const char * which will be preprended before
  330.  *   the filename, (see lines.c, label = openit).
  331.  * Relevant wims_environment parameters:
  332.  * - wims_read_parm  (should be restored to its original
  333.  *   value at the end of the procedure)
  334.  * - wims_trustfile: this parameter is only read.
  335.  * Important procedures called by this one:
  336.  * - phtml_put(char*, int)
  337.  * - var_proc(char*, int)
  338.  * - module_error(const char*)
  339.  * @param p pointer to the filename, possibly followed by parameters
  340.  */
  341.  
  342. void exec_read(char *p)
  343. {
  344.     WORKING_FILE save;
  345.     char parmsave[MAX_LINELEN+1];
  346.     char *pp,*ps;
  347.     int t,cache;
  348.  
  349.     strip_trailing_spaces(p);p=find_word_start(p);
  350.     if(*p==0) return;
  351.     pp=find_word_end(p); if(*pp) *pp++=0;
  352.     pp=find_word_start(pp);
  353.     ps=getvar("wims_read_parm"); if(ps==NULL) ps="";
  354.     mystrncpy(parmsave,ps,sizeof(parmsave));
  355.     setvar("wims_read_parm",pp);
  356.     memmove(&save,&m_file,sizeof(WORKING_FILE));
  357.     cache=1; if(p[0]=='.' && p[1]=='/') {p+=2; cache=0;}
  358.     t=untrust;
  359.     if(strncmp(p,"wimshome/",9)==0) {
  360.       if((untrust&255)!=0 &&
  361.         (m_file.name[0]=='/' || strncmp(m_file.name,"wimshome/",9)==0))
  362.         module_error("Illegal_file_access");
  363.       untrust|=0x200;
  364.     }
  365.     if((untrust&0x202)!=0) {
  366.       pp=getvar("wims_trustfile");
  367.       if(pp!=NULL && *pp!=0 && wordchr(pp,p)!=NULL)
  368.         untrust&=0xfdfd;
  369.     }
  370.     readnest++; if(readnest>MAX_READNEST) module_error("too_many_nested_read");
  371.     if(outputing) phtml_put(p,cache); else var_proc(p,cache);
  372.     readnest--; untrust=t;
  373.     memmove(&m_file,&save,sizeof(WORKING_FILE));
  374.     if (trace_file)
  375.     {
  376.       int i;
  377.       putc('\n',trace_file);
  378.       for(i=1; i<=2*trace_indent-2; i++) putc(' ',trace_file);
  379.       fprintf(trace_file,"%s:",m_file.filepath); fflush(trace_file);
  380.     }
  381.     setvar("wims_read_parm",parmsave);
  382. }
  383.  
  384. /* read a variable processing file */
  385. void exec_readproc(char *p)
  386. {
  387.     int o=outputing; outputing=0; exec_read(p); outputing=o;
  388. }
  389.  
  390. void exec_defread(char *p)
  391. {
  392.     int t,o;
  393.     secure_exec();
  394.     t=untrust; untrust|=0x400;
  395.     o=outputing; outputing=0;
  396.     exec_read(p); untrust=t; outputing=o;
  397. }
  398.  
  399. /* Change to another file (no return) */
  400. void exec_changeto(char *p)
  401. {
  402.     m_file.linepointer=m_file.linecnt;
  403.     exec_read(p);
  404. }
  405.  
  406. int header_executed=0, tail_executed=0;
  407.  
  408. /* internal routine: get other language versions */
  409. void other_langs(void)
  410. {
  411.     int i,j,k;
  412.     char *p, lbuf[4], pbuf[MAX_FNAME+1], *phtml;
  413.     char namebuf[MAX_FNAME+1], listbuf[4*MAX_LANGUAGES];
  414.     static int otherlangs_got=0;
  415.  
  416.     if(otherlangs_got) return;
  417.     mystrncpy(namebuf,module_prefix,sizeof(namebuf));
  418.     listbuf[0]=0;j=strlen(namebuf)-3; p=listbuf;
  419.     if(j>=2 && strcmp("light",namebuf+j-2)==0) {
  420.       setvar("wims_light_module","yes");
  421.       phtml=getvar("phtml");
  422.       if(phtml==NULL || *phtml==0) return;
  423.       mystrncpy(pbuf,phtml,sizeof(pbuf));
  424.       j=strlen(pbuf)-3;
  425.       if(pbuf[j]!='.') return;
  426.       j++; ovlstrcpy(lbuf,pbuf+j); pbuf[j]=0;
  427.       j=strlen(namebuf);
  428.       snprintf(namebuf+j,MAX_FNAME-j-10,"/pages/%s",pbuf);
  429.       j=strlen(namebuf);
  430.       for(i=k=0;i<available_lang_no;i++) {
  431.         if(strcmp(lbuf,available_lang[i])==0) continue;
  432.         mystrncpy(namebuf+j,available_lang[i],MAX_FNAME-j);
  433.         if(ftest(namebuf)<0) continue;
  434.         if(k>0) *p++=',';
  435.         mystrncpy(p,available_lang[i],sizeof(listbuf)-4-(p-listbuf));
  436.         p+=strlen(p);
  437.         k++;
  438.       }
  439.     goto end;
  440.     }
  441.     if(j>0 && namebuf[j]=='.') {
  442.       setvar("wims_light_module","");
  443.       j++; ovlstrcpy(lbuf,namebuf+j); namebuf[j]=0;
  444.       for(i=k=0;i<available_lang_no;i++) {
  445.         if(strcmp(lbuf,available_lang[i])==0) continue;
  446.         snprintf(namebuf+j,MAX_FNAME-j,"%s/main.phtml",
  447.              available_lang[i]);
  448.         if(ftest(namebuf)<0) continue;
  449.         if(k>0) *p++=',';
  450.         mystrncpy(p,available_lang[i],sizeof(listbuf)-4-(p-listbuf));
  451.         p+=strlen(p);
  452.         k++;
  453.       }
  454.     }
  455.     end: setvar("wims_otherlangs",listbuf); otherlangs_got=1;
  456. }
  457.  
  458. /* Standardised reference to wims home */
  459. void exec_homeref(char *p)
  460. {
  461.     char *ref, *user;
  462.  
  463.     if(ismhelp || tail_executed) return;
  464.     setvar("wims_homeref_parm",p); *p=0;
  465.     user=getvar("wims_user");
  466.     if(user==NULL) user="";
  467.     if(*user==0 || robot_access) ref=home_referer;
  468.     else {
  469.       if(strcmp(user,"supervisor")==0) ref=home_referer_supervisor;
  470.       else ref=home_referer_user;
  471.     }
  472.     if(user[0]==0 && !robot_access) other_langs();
  473.     phtml_put_base(ref,0); tail_executed=1;
  474. }
  475.  
  476. /* Standardised header menu */
  477. void exec_headmenu(char *p)
  478. {
  479.     char *ref, *user;
  480.  
  481.     if(header_executed) return;
  482.     setvar("wims_headmenu_parm",p); *p=0;
  483.     user=getvar("wims_user");
  484.     if(user==NULL) user="";
  485.     if(*user==0 || robot_access) ref=header_menu;
  486.     else {
  487.       if(strcmp(user,"supervisor")==0) ref=header_menu_supervisor;
  488.       else ref=header_menu_user;
  489.     }
  490.     if(user[0]==0 && !robot_access) other_langs();
  491.     phtml_put_base(ref,0); header_executed=1;
  492. }
  493.  
  494. /* uniformized title */
  495. void exec_title(char *p)
  496. {
  497.     char *s;
  498.     *p=0;
  499.     if(!outputing) return;
  500.     s=getvar("wims_title_title");
  501.     if(s==NULL || *s==0) {
  502.       s=getvar("module_title");
  503.       if(s==NULL || *s==0) return;
  504.       force_setvar("wims_title_title",s);
  505.     }
  506.     phtml_put_base(title_page,0);
  507. }
  508.  
  509. /* standardized html tail */
  510. void exec_tail(char *p)
  511. {
  512.     if(!outputing || tail_executed) {
  513.       *p=0; return;
  514.     }
  515.     if(!ismhelp) exec_homeref(p);
  516.     *p=0;
  517.     _output_("</body></html>");
  518.     tail_executed=1;
  519. }
  520.  
  521. void exec_formend(char *p)
  522. {
  523.     _output_("\n</div></form>");
  524. }
  525.  
  526. void determine_font(char *l);
  527.  
  528. /* see changelog wims_mathml for history */
  529. void _headmathjax ( char *p)
  530. {
  531.    _output_("\n<script type=\"text/javascript\">/*<![CDATA[*/\
  532. function wims_mathml_zoom(id){\
  533. var math = document.getElementById(id);\
  534. if(math.getAttribute(\"mathsize\") == \"100%\"){\
  535.  math.setAttribute(\"mathsize\",\"200%\");}else{\
  536.  math.setAttribute(\"mathsize\",\"100%\");};\
  537. };\
  538. var ua = navigator.userAgent.toLowerCase();\
  539. var gecko = ua.indexOf(\"gecko\") > -1 && ua.indexOf(\"khtml\") === -1 && ua.indexOf(\"trident\") === -1;\
  540. if(! gecko ){\
  541. var script = document.createElement(\"script\");\
  542. script.type = \"text/javascript\";\
  543. script.src  = \"scripts/js/mathjax/MathJax.js?config=MML_HTMLorMML-full.js\";\
  544. document.body.appendChild(script);\
  545. };/*]]>*/</script>\n");
  546. }
  547.  
  548. /* standardized header */
  549. void _header(char *p, int option)
  550. {
  551.     char *s1, *s2, hbuf[MAX_LINELEN+1], *ws="", *ws2="", *bo, *ol;
  552.     char wsbuf[MAX_LINELEN+1],wsbuf2[MAX_LINELEN+1];
  553.     setvar("wims_header_parm",p); *p=0;
  554.     if(!outputing || header_executed) return;
  555.     s1=getvar("wims_window");
  556.     if(mode==mode_popup) {
  557.       if(s1!=NULL && *s1!=0) {
  558.         char *p1, *p2;
  559.         int t1,t2/*,t3,t4*/;
  560.         p1=find_word_start(s1);
  561.         for(p2=p1; myisdigit(*p2); p2++);
  562.         *p2=0; t1=atoi(p1); p1=p2+1;
  563.         while(!myisdigit(*p1) && *p1) p1++;
  564.         for(p2=p1; myisdigit(*p2); p2++);
  565.         *p2=0; t2=atoi(p1); p1=p2+1;
  566. /*        while(!myisdigit(*p1) && *p1) p1++;
  567.         for(p2=p1; myisdigit(*p2); p2++);
  568.         *p2=0; t3=atoi(p1); p1=p2+1;
  569.         while(!myisdigit(*p1) && *p1) p1++;
  570.         for(p2=p1; myisdigit(*p2); p2++);
  571.         *p2=0; t4=atoi(p1); p1=p2+1;
  572.         while(!myisdigit(*p1) && *p1) p1++;
  573.         for(p2=p1; myisdigit(*p2); p2++);
  574.         if(t3<5) t3=5; if(t4<20) t4=20;
  575. */      snprintf(wsbuf,sizeof(wsbuf),
  576.           "window.focus();window.resizeTo(%d,%d);",t1,t2); ws=wsbuf;
  577. /*      snprintf(wsbuf,sizeof(wsbuf),
  578.              "window.focus();window.resizeto(%d,%d);window.moveto(%d,%d);",
  579.              t1,t2,t3,t4); ws=wsbuf;
  580. */    }
  581.     }
  582.     else {
  583.      if(s1!=NULL && strcmp(s1,"new")==0)
  584.       ws="window.focus();window.resizeTo(800,640);window.moveTo(15,35);";
  585.     }
  586.     if(strstr(session_prefix,"_exam")!=NULL) {
  587. /*    char buf[64]; */
  588.       if(*ws==0) ws="window.focus();";
  589.       else ws= "window.focus();window.moveTo(5,70);";
  590. /*    snprintf(buf,sizeof(buf),"name.phtml.%s",lang);
  591.       phtml_put_base(buf);
  592.       phtml_put_base("jsclock.phtml"); */
  593.     }
  594.     s1=getvar("wims_html_header"); if(s1==NULL) s1="";
  595.     determine_font(getvar("module_language"));
  596.     s2=getvar("module_title"); if(s2!=NULL && *s2!=0) {
  597.       mystrncpy(hbuf,s2,sizeof(hbuf)); calc_detag(hbuf);
  598.       setvar("module_title2",hbuf);
  599.     }
  600.     mystrncpy(hbuf,s1,sizeof(hbuf)); substit(hbuf);
  601.     s2=getvar("wims_htmlbody"); if(s2==NULL) s2="";
  602.     bo=getvar("wims_html_bodyoption"); if(bo==NULL) bo="";
  603.     ws2=getvar("wims_html_onload"); if(ws2==NULL) ws2="";
  604.     snprintf(wsbuf2,sizeof(wsbuf2),"%s%s",ws,ws2);
  605.     setvar("wims_html_onload",wsbuf2);
  606.     if(wsbuf2[0]) ol=" onload="; else ol="";
  607. /*    output("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>%s\n\
  608. </head><body %s %s%s %s>\n", */
  609. /*    output("<html>\n\
  610. <head>%s\n\
  611. </head><body %s %s%s %s>\n",
  612.        hbuf,s2,ol,wsbuf2,bo);*/
  613.        /*http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd*/
  614.      if(ol[0]) {
  615.         output("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\" \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\">\
  616.        \n<html xml:lang=\"%s\"\
  617.        xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:mathml=\"http://www.w3.org/1998/Math/MathML\"><head>%s\n\
  618.     </head>\n<body %s %s\"%s\" %s>", lang,hbuf,s2,ol,wsbuf2,bo);}
  619.      else {
  620.        output("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\" \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\">\
  621.        \n<html xml:lang=\"%s\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:mathml=\"http://www.w3.org/1998/Math/MathML\"><head>%s\n\
  622.        </head>\n<body %s %s%s %s>", lang,hbuf,s2,ol,wsbuf2,bo);
  623.     }
  624.     _headmathjax(p);
  625.     exec_headmenu(p);
  626.     if(option) exec_title(p);
  627.     if(cmd_type==cmd_help) {
  628.       char *s=getvar("special_parm");
  629.       if(s==NULL) s="";
  630.       m_file.linepointer=m_file.linecnt;
  631.       if(strcmp(s,"about")==0) ovlstrcpy(hbuf,"about.phtml");
  632.       else ovlstrcpy(hbuf,"help.phtml");
  633.       exec_read(hbuf); exec_tail(p); /* param of exec_...() must be readable */
  634.      return;
  635.     }
  636.     header_executed=1;
  637. }
  638.  
  639. void exec_header(char *p) { _header(p,1);}
  640. void exec_header1(char *p) { _header(p,0);}
  641.  
  642. char href_target[128];
  643. char jsbuf[512];
  644. #define jsstr " onclick=\"%s=window.open('','%s','status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes')\""
  645. int ref_mhelp=0;
  646. int follow_list[]={
  647.     ro_session, ro_lang, ro_useropts, ro_module
  648. };
  649. #define follow_no (sizeof(follow_list)/sizeof(follow_list[0]))
  650.  
  651. void _httpfollow(char b1[], char *wn, int new)
  652. {
  653.     int i;
  654.     char *p1, *s, *ss, sb[MAX_LINELEN+1], qbuf[MAX_LINELEN+1];
  655.  
  656.     sb[0]=0;
  657.     for(i=0;i<follow_no;i++) {
  658.       if(robot_access && follow_list[i]==ro_session) continue;
  659.       if(!new && follow_list[i]!=ro_session
  660.        && follow_list[i]!=ro_module && follow_list[i]!=ro_lang)
  661.        continue;
  662.       if(follow_list[i]==ro_module) {
  663.         char *pp;
  664.         if(new) continue;
  665.         pp=strstr(b1,"cmd=");
  666.         if(pp==NULL) continue;
  667.         pp+=strlen("cmd=");
  668.         if(memcmp(pp,"intro",strlen("intro"))==0 ||
  669.            memcmp(pp,"new",strlen("new"))==0) continue;
  670.       }
  671.       s=getvar(ro_name[follow_list[i]]);
  672.       ss=strstr(b1,ro_name[follow_list[i]]);
  673.       if(s!=NULL && *s!=0 &&
  674.         (ss==NULL || (ss>b1 && *(ss-1)!='&')
  675.          || *(ss+strlen(ro_name[follow_list[i]]))!='=')) {
  676.         if(follow_list[i]==ro_session && memcmp(href_target,"wims_",5)==0) {
  677.          char st[MAX_LINELEN+1];
  678.          char *s1;
  679.          s1=getvar("wims_session");
  680.          if(s1==NULL) internal_error("exec_href() error.\n");
  681.          if(ref_mhelp) {
  682.             if(strstr(s1,"_mhelp")!=0)
  683.               snprintf(st,sizeof(st),"%s",s1);
  684.             else
  685.               snprintf(st,sizeof(st),"%s_mhelp",s1);
  686.             }
  687.         else snprintf(st,sizeof(st),"%.10s%s",s1,href_target+4);
  688.           s=st;
  689.         }
  690.         snprintf(sb+strlen(sb),MAX_LINELEN-strlen(sb),"%s=%s&",
  691.              ro_name[follow_list[i]],s);
  692.         if(ismhelp && follow_list[i]==ro_session &&
  693.            strstr(sb,"_mhelp")==NULL)
  694.           snprintf(sb+strlen(sb)-1,MAX_LINELEN-strlen(sb)+1,"_mhelp&");
  695.        }
  696.     }
  697.     snprintf(qbuf,MAX_LINELEN,"%s%s%s",wn,sb,b1);
  698.     /* cleaning up query string */
  699.     for(p1=qbuf;*p1;p1++) {
  700.       if(*p1=='"') string_modify(qbuf,p1,p1+1,"%22");
  701.       if(*p1=='&' && isalpha(*(p1+1))) {
  702.         p1++; string_modify(qbuf,p1,p1,"+");
  703.       }
  704.     }
  705.     mystrncpy(b1,qbuf,MAX_LINELEN);
  706. }
  707.  
  708. /* Restart with a new module, using http code 302. */
  709. void exec_restart(char *p)
  710. {
  711.     char buf[MAX_LINELEN+1], *rfn, buf2[MAX_LINELEN+1];
  712. /*    long int t; */
  713.  
  714.     if(robot_access || outputing || !trusted_module() || is_class_module) return;
  715. /*    accessfile(buf,"r","%s/restart.time",s2_prefix);
  716.     t=atoi(buf); if(t==nowtime) return;    */ /* possible looping */
  717.     mystrncpy(buf,find_word_start(p),sizeof(buf));
  718.     *find_word_end(buf)=0;
  719.     _httpfollow(buf,"",0);
  720.     nph_header(301);
  721.     rfn=strchr(ref_name,':'); if(rfn==NULL) {
  722.       usual: snprintf(buf2,sizeof(buf2),"%s?%s",ref_name,buf);
  723.     }
  724.     else {
  725.       char *p;
  726.       p=getvar("wims_protocol");
  727.       if(p!=NULL && strcmp(p,"https")==0) {
  728.         snprintf(buf2,sizeof(buf2),"https%s?%s",rfn,buf);
  729.       }
  730.       else goto usual;
  731.     }
  732.     printf("Location: %s\r\n\r\n\
  733. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\
  734. <html><body><a href=\"%s\">%s</a></body></html>",buf2,buf2,buf2);
  735.     close_working_file(&m_file,0); write_logs();
  736.     snprintf(buf,sizeof(buf),"%ld",(long)nowtime);
  737. /*    accessfile(buf,"w","%s/restart.time",s2_prefix);
  738. */    delete_pid(); exit(0);
  739. }
  740.  
  741. /* extract target tag from parm string. */
  742. void href_find_target(char *p)
  743. {
  744.     char *pp, *pe,buf1[MAX_LINELEN+1];
  745.     href_target[0]=0; jsbuf[0]=0; ref_mhelp=0;
  746.     for(pp=find_word_start(p);*pp!=0;pp=find_word_start(pe)) {
  747.       pe=find_word_end(pp);
  748.       if(strncasecmp(pp,"target=wims_",strlen("target=wims_"))!=0) continue;
  749.       memmove(buf1,pp,pe-pp); buf1[pe-pp]=0; substit(buf1);
  750.       if(strncasecmp(buf1,"target=wims_mhelp",strlen("target=wims_mhelp"))==0) {
  751.         if(*pe!=0) *pe++=0;
  752.         ovlstrcpy(href_target,"wims_help");
  753.         ref_mhelp=1;
  754.       }
  755.       else {
  756.         if(*pe!=0) *pe++=0;
  757.           mystrncpy(href_target,buf1+strlen("target="),sizeof(href_target));
  758.       }
  759.       snprintf(jsbuf,sizeof(jsbuf),jsstr,href_target,href_target);
  760.       ovlstrcpy(pp,pe);return;
  761.     }
  762.     pp=getvar("module_help");
  763.     if(href_target[0]==0 && pp!=NULL && strcmp(pp,"popup")==0 &&
  764.        (pe=strstr(p,"cmd=help"))!=NULL) {
  765.       if(pe==p || *(pe-1)=='&') {
  766.         ovlstrcpy(href_target,"wims_help"); ref_mhelp=1;
  767.         snprintf(jsbuf,sizeof(jsbuf),jsstr,href_target,href_target);
  768.       }
  769.     }
  770. }
  771.  
  772. void _href_getdef(char src[], char vname[], char buf[], int buflen)
  773. {
  774.     char *p1, *p2, *p3;
  775.     buf[0]=0;
  776.     for(p1=strstr(src,vname); p1; p1=strstr(p2,vname)) {
  777.       p2=p1+strlen(vname); if(*p2!='=') continue;
  778.       if(p1>src && *(p1-1)!='&') continue;
  779.       p2++; p3=strchr(p2,'&'); if(p3==NULL) p3=p2+strlen(p2);
  780.       if(p3-p2>=buflen) return; /* too long */
  781.       memmove(buf,p2, p3-p2); buf[p3-p2]=0; return;
  782.     }
  783. }
  784.  
  785. /* Create href to wims requests. subst() is not done. */
  786. void exec_href(char *p)
  787. {
  788.     char *s, st[128], sti[128], stc[128], stt[128], *p1, *p2, *p3, *wn="";
  789.     char *U="<span class=\"disabled_link\">%s</span>";
  790.     char b1[MAX_LINELEN+1], b2[MAX_LINELEN+1];
  791.     int new=0;
  792.     if(!outputing) return;
  793.     href_find_target(p);
  794.     p1=find_word_start(p);
  795.     p2=find_word_end(p1); if(*p2) *(p2++)=0;
  796.     mystrncpy(b1,p1,sizeof(b1));
  797.     mystrncpy(b2,find_word_start(p2),sizeof(b2));
  798.     substit(b1); substit(b2);
  799.     /* standard reference */
  800.     if(*b2==0 && strchr(b1,'=')==NULL) {
  801.       char b[MAX_LINELEN+1], *ll;
  802.       p1=find_word_start(b1); *find_word_end(p1)=0;
  803.       if(*p1==0 || strlen(p1)>64) return;
  804.       ll=getvar("module_language");
  805.       if(ll==NULL || *ll==0 || *(ll+1)==0 || *(ll+2)!=0) ll=lang;
  806.       accessfile(b,"r","html/href.%s",ll);
  807.       memmove(p1+1,p1,64); *p1='\n'; strcat(p1,"        ");
  808.       p2=strstr(b,p1); if(p2==NULL) return;
  809.       p1=find_word_start(p2+strlen(p1)); p2=find_word_end(p1);
  810.       if(*p2) *(p2++)=0;
  811.       p3=strchr(p2,'\n'); if(p3!=NULL) *p3=0;
  812.       mystrncpy(b1,p1,sizeof(b1));
  813.       mystrncpy(b2,find_word_start(p2),sizeof(b2));
  814.       substit(b1); substit(b2);
  815.     }
  816.     /* for robots: only references without defining cmd. */
  817.     if(robot_access && strstr(b1,"cmd=")!=NULL &&
  818.        strstr(b1,"module=adm/doc")==NULL) {
  819.       _output_(b2); return;
  820.     }
  821.     if(robot_access && strstr(aliased_cgi,"yes")!=NULL) {
  822.       char mbuf[256], lbuf[16];
  823.       _href_getdef(b1,"module",mbuf,sizeof(mbuf));
  824.       if(mbuf[0]==0) mystrncpy(mbuf,home_module,sizeof(mbuf));
  825.       _href_getdef(b1,"lang",lbuf,sizeof(lbuf));
  826.       if(strlen(lbuf)!=2) {mystrncpy(lbuf,lang,4);lbuf[2]=0;}
  827.       if(strncmp(mbuf,"adm/doc",strlen("adm/doc"))==0) {
  828.         char dbuf[256], bbuf[256];
  829.         _href_getdef(b1,"doc",dbuf,sizeof(dbuf));
  830.         _href_getdef(b1,"block",bbuf,sizeof(bbuf));
  831.         if(!myisdigit(dbuf[0])) dbuf[0]=0;
  832.         if(dbuf[0]!=0 && bbuf[0]==0) snprintf(bbuf,sizeof(bbuf),"main");
  833.         if(dbuf[0]==0)
  834.           output("<a href=\"%s%s_doc~.html\">%s</a>", ref_base,lbuf,b2);
  835.         else
  836.           output("<a href=\"%s%s_doc~%s~%s.html\">%s</a>",
  837.              ref_base,lbuf,dbuf,bbuf,b2);
  838.       }
  839.       else {
  840.         for(s=strchr(mbuf,'/'); s!=NULL; s=strchr(s+1,'/')) *s='~';
  841.         output("<a href=\"%s%s_%s.html\">%s</a>", ref_base,lbuf,mbuf,b2);
  842.       }
  843.     return;
  844.     }
  845.     s=getvar("wims_ref_id");
  846.     if(s!=NULL && *s!=0 && !isspace(*s)) {
  847.       snprintf(sti,sizeof(sti)," id=\"%s\"",s);
  848.     }
  849.     else sti[0]=0;
  850.  
  851.     s=getvar("wims_ref_class");
  852.     if(s!=NULL && *s!=0 && !isspace(*s)) {
  853.       snprintf(stc,sizeof(stc)," class=\"%s\"",s);
  854.     }
  855.     else stc[0]=0;
  856.  
  857.     s=getvar("wims_ref_title");
  858.     if(s!=NULL && *s!=0 && !isspace(*s)) {
  859.       snprintf(stt,sizeof(stt)," title=\"%s\"",s);
  860.     }
  861.     else stt[0]=0;
  862.  
  863.     s=getvar("wims_ref_target");
  864.     if(href_target[0]!=0) s=href_target;
  865.     if(s!=NULL && *s!=0 && !isspace(*s)) {
  866.       snprintf(st,sizeof(st)," target=\"%s\"",s);
  867.       if(strcmp(s,"_parent")!=0) {
  868.          new=1; wn="wims_window=new&";
  869.       }
  870.     }
  871.     else st[0]=0;
  872.     _httpfollow(b1,wn,new);
  873.     tohttpquery(b1);
  874.     if(strstr(session_prefix,"_check")!=NULL) {
  875.       if(*b2) output(U,b2);
  876.     else _output_("<a id=\"0\"></a>");
  877.       return;
  878.     }
  879.     if(jsbuf[0]==0 && st[0]==0 && strstr(session_prefix,"_exam")!=NULL) {
  880.       p1=strstr(b1,"cmd=");
  881.       if(p1!=NULL) {
  882.         p1+=strlen("cmd=");
  883.         if(strncmp(p1,"new",3)==0 || strncmp(p1,"renew",5)==0 ||
  884.            strncmp(p1,"intro",5)==0) {
  885.          if(*b2) output(U,b2);
  886.          else _output_("<a id=\"#\"></a>");
  887.           return;
  888.         }
  889.       }
  890.     }
  891.     if(*b2)
  892.       output("<a href=\"%s?%s\"%s%s %s %s %s>%s</a>",
  893.          ref_name, b1, st, jsbuf,sti,stc,stt,b2);
  894.     else
  895.       output("<a href=\"%s?%s\"%s%s %s %s %s>",ref_name, b1, st, jsbuf,sti,stc,stt);
  896.     setvar("wims_ref_id","");
  897.     setvar("wims_ref_class","");
  898.     setvar("wims_ref_title","");
  899. }
  900.  
  901. /* Create form refering to the page. */
  902. void exec_form(char *p)
  903. {
  904.     char *s, *p1, *p2, *a, *m, *opt, st[128], *wn="";
  905.     char abuf[128];
  906.     int i, new=0;
  907.     if(!outputing) return;
  908.     href_find_target(p);
  909.     s=getvar("wims_ref_target");
  910.     if(href_target[0]!=0) s=href_target;
  911.     if(s!=NULL && *s!=0 && !isspace(*s)) {
  912.       snprintf(st,sizeof(st)," target=\"%s\"",s);
  913.       if(strcmp(s,"_parent")!=0) {
  914.         new=1; wn="<input type=\"hidden\" name=\"wims_window\" value=\"yes\" />\n";
  915.       }
  916.     }
  917.     else st[0]=0;
  918.     a=getvar("wims_ref_anchor"); if(a==NULL) a="";
  919.     opt=find_word_start(find_word_end(find_word_start(p)));
  920.     m=getvar("wims_form_method");
  921.     if(m!=NULL) {
  922.       m=find_word_start(m);
  923.       if(strncasecmp(m,"post",4)==0) m="post";
  924.       else if(strncasecmp(m,"get",3)==0) m="get";
  925.         else if(strncasecmp(m,"file",4)==0) {
  926.          m="post\" enctype=\"multipart/form-data";
  927.         snprintf(abuf,sizeof(abuf),"?form-data%ld%s",random(),a); a=abuf;
  928.         force_setvar("wims_form_method","");
  929.      }
  930.       else m=default_form_method;
  931.     }
  932.     else m=default_form_method;
  933.     if(strstr(session_prefix,"_check")!=NULL) {
  934.       output("<form action=\"NON_EXISTING_PAGE\" onsubmit=\"window.close();\" %s>\n",
  935.            opt);
  936.       return;
  937.     }
  938.     output("<form action=\"%s%s\"%s method=\"%s\" %s>\n<div class='wims_form'>%s",ref_name,a,st,m,opt,wn);
  939.     if(a!=abuf && a[0]) force_setvar("wims_ref_anchor","");
  940.     for(i=0;i<follow_no;i++) {
  941.       if(robot_access && follow_list[i]==ro_session) continue;
  942.       if(!new && follow_list[i]!=ro_session
  943.        && follow_list[i]!=ro_module && follow_list[i]!=ro_lang)
  944.         continue;
  945.       if(follow_list[i]==ro_module) continue;
  946.       s=getvar(ro_name[follow_list[i]]);
  947.       if(s!=NULL && *s!=0) {
  948.         if(follow_list[i]==ro_session && memcmp(href_target,"wims_",5)==0) {
  949.           char st[MAX_LINELEN+1];
  950.           char *s1;
  951.           s1=getvar("wims_session");
  952.           if(s1==NULL) internal_error("exec_form() error.\n");
  953.           snprintf(st,sizeof(st),"%.10s%s",s1,href_target+4);
  954.           s=st;
  955.         }
  956.         output("<input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
  957.            ro_name[follow_list[i]],s);
  958.       }
  959.     }
  960.     p1=find_word_start(p);p2=find_word_end(p1);
  961.     if(p2>p1) {
  962.       char buf[64];
  963.       int i;
  964.       i=p2-p1; if(i>60) i=60;
  965.       memmove(buf,p1,i);buf[i]=0;
  966.       for(i=0;i<CMD_NO && strcmp(buf,commands[i]);i++);
  967.       if(i<CMD_NO) {
  968.         output("<input type=\"hidden\" name=\"cmd\" value=\"%s\" />\n",buf);
  969.         if(i!=cmd_intro && i!=cmd_new)
  970.           output("<input type=\"hidden\" name=\"module\" value=\"%s\" />\n",
  971.              getvar(ro_name[ro_module]));
  972.       }
  973.    }
  974. }
  975.  
  976. /* Creat link to trap robot access, an internal command
  977.  * which should not be documented
  978.  */
  979. void exec_robottrap(char *p)
  980. {
  981.     char buf[MAX_LINELEN+1];
  982.     if(robot_access) return;
  983.     ovlstrcpy(buf,"session=$wims_session.1&module=adm/trap");
  984.     _output_("<!-- "); exec_href(buf);_output_("Robot trapper, do not click!</a> -->");
  985.     _output_("<div class='wimstrap'>");exec_href(buf); _output_("<span></span></a></div>");
  986. }
  987.  
  988. /* set definitions in a file. Trusted modules only. */
  989. void exec_setdef(char *p)
  990. {
  991.     char *p1, *pp;
  992.     char nbuf[MAX_LINELEN+1], fbuf[MAX_LINELEN+1], tbuf[MAX_LINELEN+1];
  993.     if(robot_access || !trusted_module() || is_class_module) return;
  994.     p1=wordchr(p,"in"); if(p1==NULL) module_error("syntax_error");
  995.     *p1=0; p1=find_word_start(p1+strlen("in"));
  996.     ovlstrcpy(nbuf,p);
  997.     mystrncpy(tbuf,p1,sizeof(tbuf));
  998.     substit(nbuf); substit(tbuf);
  999.     if(find_module_file(tbuf,fbuf,1)) return;
  1000.     pp=find_word_start(nbuf); p1=find_word_start(fbuf); *find_word_end(p1)=0;
  1001.     strip_trailing_spaces(pp);
  1002.     setdef(p1,pp);
  1003. }
  1004.  
  1005. /* Set a variable. */
  1006. void exec_set(char *name)
  1007. {
  1008.     char *p, *defn, *parm;
  1009.     char tbuf2[MAX_LINELEN+1], namebuf[MAX_LINELEN+1];
  1010.     int i;
  1011.  
  1012.     p=strchr(name,'=');
  1013.     if(p==NULL) return; /* warning or error! */
  1014.     *p=0; defn=find_word_start(p+1);
  1015.     *find_word_end(name)=0;
  1016.     mystrncpy(namebuf,find_word_start(name),sizeof(namebuf));
  1017.     /* we allow substit in names, to implement array */
  1018.     substit(namebuf); *find_word_end(namebuf)=0;
  1019.     if(*defn!=calc_prefix_char) {
  1020.     /* substitute by default */
  1021.       mystrncpy(tbuf2,defn,sizeof(tbuf2));
  1022.       substit(tbuf2); setvar(namebuf,tbuf2); return;
  1023.     }
  1024.     /* called from !readdef */
  1025.     if((untrust&4)!=0) module_error("not_trusted");
  1026.     /* definition is a command  */
  1027.     parm=find_word_end(defn+1);
  1028.     if( *parm != 0 ) { *parm=0; parm=find_word_start(parm+1); }
  1029.     i=m_file.lines[m_file.l].varcode;
  1030.     if(i<0) {
  1031.       i=search_list(calc_routine,CALC_FN_NO,sizeof(calc_routine[0]),defn+1);
  1032.       m_file.lines[m_file.l].varcode=i;
  1033.     }
  1034.     if(i<0) {
  1035.         /* replace by warning? */
  1036.       setvar(error_data_string,defn+1); module_error("bad_cmd");
  1037.       return;
  1038.     }
  1039.     mystrncpy(tbuf2,parm,sizeof(tbuf2)); execnt++;
  1040.     if(calc_routine[i].tag==0) substit(tbuf2);
  1041.     tbuf2[sizeof(tbuf2)-1]=0; calc_routine[i].routine(tbuf2);
  1042.         /* remove trailing new line */
  1043.     tbuf2[sizeof(tbuf2)-1]=0;
  1044.     if(tbuf2[strlen(tbuf2)-1]=='\n') tbuf2[strlen(tbuf2)-1]=0;
  1045.     setvar(namebuf,tbuf2);
  1046. }
  1047.  
  1048. /* set but do not overwrite. */
  1049. void exec_default(char *p)
  1050. {
  1051.     char *start, *end, c, *pp;
  1052.     char namebuf[MAX_LINELEN+1];
  1053.     start=find_word_start(p);
  1054.     for(end=start;*end!=0 && !isspace(*end) && *end!='='; end++);
  1055.     c=*end; *end=0;
  1056.     if(end-start<=MAX_LINELEN-1) {
  1057.       memmove(namebuf,start,end-start+1); substit(namebuf);
  1058.       pp=getvar(namebuf);
  1059.       if(pp!=NULL && *pp!=0) return;
  1060.     }
  1061.     *end=c; exec_set(p);
  1062. }
  1063.  
  1064. /* Does nothing; just a comment. */
  1065. void exec_comment(char *p)
  1066. {
  1067.     return;
  1068. }
  1069.  
  1070. /* Exit the file under interpretation */
  1071. void exec_exit(char *p)
  1072. {
  1073.     m_file.linepointer=m_file.linecnt;
  1074.     return;
  1075. }
  1076.  
  1077. /* output a file. Undocumented. Aliases:
  1078.      * getfile, outfile, fileout */
  1079. void exec_getfile(char *p)
  1080. {
  1081.     char *s, *p1, url[MAX_LINELEN+1];
  1082.     char *prompt;
  1083.  
  1084.     p=find_word_start(p); prompt=find_word_end(p);
  1085.     if(*prompt!=0) *prompt++=0;
  1086.     prompt=find_word_start(prompt);
  1087.     if(*p==0 || !outputing) return;
  1088.     if(!trusted_module() || is_class_module) return;
  1089.     s=getvar(ro_name[ro_session]);
  1090.     if(s==NULL || *s==0 || strstr(s,"robot")!=NULL) return;
  1091.     mystrncpy(url,ref_name,sizeof(url));
  1092.     for(p1=url+strlen(url);p1>url && *(p1-1)!='/'; p1--);
  1093.     if(good_httpd) snprintf(p1,sizeof(url)+p1-url,
  1094.                 "getfile/%s?&+session=%s&+modif=%ld",
  1095.                 p,s,(long)nowtime);
  1096.     else snprintf(url,sizeof(url),
  1097.           "%s?cmd=getfile&+session=%s&+special_parm=%s&+modif=%ld",
  1098.           ref_name,s,p,(long)nowtime);
  1099.     snprintf(jsbuf,sizeof(jsbuf),jsstr,"wims_file","wims_file");
  1100.     if(*prompt) output("<a href=\"%s\">%s</a>\n", url,prompt);
  1101.     else output("<a href=\"%s\"></a>",url);
  1102. }
  1103.  
  1104. /* internal */
  1105. void count_insert(void)
  1106. {
  1107.     insert_no++;
  1108.     if(insert_no>=INS_LIMIT) module_error("too_many_ins");
  1109. }
  1110.  
  1111. int animated_ins=0;
  1112. int grouped_ins=0;
  1113.  
  1114. /* generic insertion */
  1115. void _exec_ins(char *p, char *script_name,char *format)
  1116. {
  1117.     char *s, *b, *at, *tag, *tag2, *al, *fmt, *mh;
  1118.     char *p1, *pt;
  1119.     char buf[1024],buf2[1024],url[MAX_LINELEN+1],altbuf[1024];
  1120.     char outbuf[1024];
  1121.     int border, middle, vspace;
  1122.     long int tel;
  1123.  
  1124.     if(robot_access) return;
  1125.     count_insert(); outbuf[0]=0;
  1126.     setenv("ins_source",p,1); /* value kept from user tamper */
  1127.     if(animated_ins) fmt=getvar("anim_format"); else fmt=format;
  1128.     if(fmt==NULL) fmt="gif";
  1129.     if(ismhelp) mh="mh"; else mh="";
  1130.     snprintf(buf,sizeof(buf),"%s/insert%s-%d.%s",s2_prefix,mh,insert_no,fmt);
  1131.     if(grouped_ins) {unlink(buf); goto grouped;}
  1132.     exportall();
  1133.     call_ssh("%s/%s %d %s >%s/ins.out 2>%s/ins.err",
  1134.         bin_dir,script_name,insert_no,tmp_dir,tmp_dir,tmp_dir);
  1135.     unlink(buf); wrapexec=1;
  1136.     if(trusted_module()) setenv("trusted_module","yes",1);
  1137.     else if(untrust) setenv("trusted_module","no",1);
  1138.     call_ssh("mv %s/insert%s-%d.%s %s >/dev/null 2>/dev/null",
  1139.          tmp_dir,mh,insert_no,fmt,s2_prefix);
  1140.     tel=filelength("%s", buf);
  1141.     if(tel<=5) {
  1142.       char bbuf[MAX_LINELEN+1];
  1143.       accessfile(bbuf,"r","%s/ins.err",tmp_dir);
  1144.       snprintf(url,sizeof(url),"gifs/badins.gif");
  1145.       for(p1=bbuf;p1<bbuf+512 && *p1;p1++)
  1146.       if(*p1=='<' || *p1=='>') *p1='?';
  1147.       *p1=0;
  1148.       if(bbuf[0]==0) snprintf(bbuf,sizeof(bbuf),"Fail");
  1149.       snprintf(outbuf+strlen(outbuf),sizeof(outbuf)-strlen(outbuf),
  1150.          " <img src=\"%s\" alt=\"Error\" /> <small><pre>%s</pre></small> <br /> ",
  1151.          url,bbuf);
  1152.       setvar("ins_warn","fail");
  1153.       setvar("ins_cnt","0");
  1154.       goto reset;
  1155.     }
  1156.     grouped:
  1157.     s=getvar(ro_name[ro_session]);
  1158.     b=getvar("ins_border"); at=getvar("ins_attr");
  1159.     tag=getvar("ins_tag");  al=getvar("ins_align");
  1160.     if(at==NULL) at="";
  1161.     if(tag==NULL) tag="";
  1162.     if(al==NULL) al="";
  1163.     al=find_word_start(al);
  1164.     if(*al!=0) snprintf(buf2,sizeof(buf2),"vertical-align:%s",al); else buf2[0]=0;
  1165.     if(strcasecmp(al,"middle")==0) middle=1; else middle=0;
  1166.     tag2=""; vspace=0;
  1167.     if(*tag!=0) {
  1168.       mystrncpy(buf,tag,sizeof(buf)); tag=find_word_start(buf);
  1169.       tag2=find_word_end(tag);
  1170.       if(*tag2!=0) *tag2++=0;
  1171.       tag2=find_word_start(tag2);
  1172.     }
  1173.     if(b==NULL || *b==0) border=0;
  1174.     else border=atoi(b);
  1175.     if(border<0) border=0;
  1176.     if(border>100) border=100;
  1177.     if(middle) {
  1178.       snprintf(outbuf+strlen(outbuf),
  1179.          sizeof(outbuf)-strlen(outbuf),"%s",mathalign_sup1);
  1180.       vspace=2;
  1181.     }
  1182.     mystrncpy(url,ref_name,sizeof(url));
  1183.     for(p1=url+strlen(url);p1>url && *(p1-1)!='/'; p1--);
  1184.     snprintf(p1,sizeof(url)+p1-url,
  1185.          "wims.%s?cmd=getins&+session=%s&+special_parm=insert%s-%d.%s&+modif=%ld",
  1186.          fmt,s,mh,insert_no,fmt,(long)nowtime);
  1187.     if(strchr(ins_alt,'"')!=NULL || strlen(ins_alt)>256) ins_alt[0]=0;
  1188.     pt=getvar("wims_ins_alt"); if(pt==NULL) pt="";
  1189.     if(ins_alt[0] && strcmp(pt,"none")!=0)
  1190.       snprintf(altbuf,sizeof(altbuf)," alt=\"%s\"",ins_alt);
  1191.     else snprintf(altbuf,sizeof(altbuf)," alt=\"\"");
  1192.     if(strcasecmp(tag,"form")!=0) {
  1193.       snprintf(outbuf+strlen(outbuf),sizeof(outbuf)-strlen(outbuf),
  1194.          "<img src=\"%s\" style=\"border:solid;border-width:%dpx;margin-bottom:%dpx;%s\" %s %s />",
  1195.          url, border, vspace, buf2, at, altbuf);
  1196.     }
  1197.     else {
  1198.       char *n, *nend;
  1199. /* fix: add quotes at name=" " */
  1200.       if(*tag2!=0) {n="name=\"" ; nend="\"";} else {n=""; nend="";}
  1201.       snprintf(outbuf+strlen(outbuf),sizeof(outbuf)-strlen(outbuf),
  1202.          "<input type=\"image\" %s%s%s src=\"%s\" style=\"border:solid;border-width:%dpx;margin-bottom:%dpx;%s\" %s %s />",
  1203.          n,tag2,nend,url,border,vspace,buf2,at,altbuf);
  1204.     }
  1205.     if(middle) snprintf(outbuf+strlen(outbuf),
  1206.             sizeof(outbuf)-strlen(outbuf),"%s",mathalign_sup2);
  1207.     setvar("ins_warn",""); ins_alt[0]=0;
  1208.     setvar("ins_cnt",int2str(insert_no));
  1209.     reset:
  1210.     if(outputing) _output_(outbuf);
  1211.     setvar("ins_out",outbuf);
  1212.     setvar("ins_attr",""); setvar("ins_tag","");
  1213.     setvar("ins_url",url);
  1214.     snprintf(buf2,sizeof(buf2),"insert%s-%d.%s",mh,insert_no,fmt);
  1215.     setvar("ins_filename",buf2);
  1216.     animated_ins=0;
  1217. }
  1218.  
  1219. /* instex: dynamically insert tex outputs */
  1220. void exec_instex(char *p)
  1221. {
  1222.     char *ts, *tc, *f, *mh, buf[MAX_FNAME+1];
  1223.  
  1224.     if(robot_access) {
  1225.       *p=0; return;
  1226.     }
  1227.     f=instex_check_static(p); substit(p);
  1228.     if(f==NULL) {
  1229. /* Use static instex if there is no real substitution
  1230.  * and the source file is not in sessions directory.
  1231.  */
  1232.       calc_instexst(p); if(outputing) _output_(p);
  1233.       return;
  1234.     }
  1235.     if(ismhelp) mh="mh"; else mh="";
  1236.     fix_tex_size(); f="gif";
  1237.     setenv("texgif_style",instex_style,1);
  1238.     tc=getvar("instex_texheader"); if (tc) { setenv("texgif_texheader",tc,1);}
  1239.     setenv("texgif_tmpdir",tmp_dir,1);
  1240.     setenv("texgif_src",p,1);
  1241.     if(ins_alt[0]==0) mystrncpy(ins_alt,p,sizeof(ins_alt));
  1242.     mkfname(buf,"%s/insert%s-%d.gif",tmp_dir,mh,insert_no+1);
  1243.     setenv("texgif_outfile",buf,1);
  1244.     ts=getvar("wims_texsize"); tc=getvar("instex_color");
  1245.     if(lastout_file!=-1 && (tc==NULL || *tc==0) &&
  1246.        (ts==NULL || *ts==0 || strcmp(ts,"0")==0) &&
  1247.        strstr(p,"\\begin{")==NULL) {
  1248.       int ls, ln;
  1249.       char *pagebreak;
  1250.       ls=strlen(instex_src); ln=strlen(instex_fname);
  1251.       if(ls+strlen(p)>=MAX_LINELEN-256 ||
  1252.        ln+strlen(buf)>=MAX_LINELEN-16) {
  1253.         instex_flush(); ls=ln=0;
  1254.       }
  1255.       if(instex_cnt>0) pagebreak="\\pagebreak\n"; else pagebreak="";
  1256.       snprintf(instex_src+ls,MAX_LINELEN-ls,"%s %s %s %s\n",
  1257.          pagebreak,instex_style,p,instex_style);
  1258.       snprintf(instex_fname+ln,MAX_LINELEN-ln,"%s\n",buf);
  1259.       grouped_ins=1;
  1260.     }
  1261.     mkfname(buf,"%s/texgif.dvi",tmp_dir); unlink(buf);
  1262.     wrapexec=0; _exec_ins(p,instex_processor,f);
  1263.     if(grouped_ins) instex_cnt++;
  1264.     grouped_ins=0;
  1265. }
  1266.  
  1267. /* patches the gnuplot integer division (mis)feature. */
  1268. void gnuplot_patch(char *p,int oneline)
  1269. {
  1270.     char *pp;
  1271.     for(pp=strchr(p,'/');pp!=NULL;pp=strchr(pp+1,'/')) {
  1272.       char *p1;
  1273.       if(pp<=p || !myisdigit(*(pp-1)) || !myisdigit(*(pp+1))) continue;
  1274.       for(p1=pp-2;p1>=p && myisdigit(*p1);p1--);
  1275.       if(p1>=p && *p1=='.') continue;
  1276.       for(p1=pp+2;*p1 && myisdigit(*p1);p1++);
  1277.       if(*p1=='.') continue;
  1278.       string_modify(p,p1,p1,".0");
  1279.     }
  1280.     for(pp=strchr(p,'^');pp!=NULL;pp=strchr(pp+1,'^'))
  1281.       string_modify(p,pp,pp+1,"**");
  1282. /* disallow new lines and ';' */
  1283.     if(oneline)
  1284.       for(pp=p;*pp!=0;pp++) if(*pp==';' || *pp=='\n') *pp=' ';
  1285. }
  1286.  
  1287. /* This is to disable pipe in the gnuplot plotting function.
  1288.  * We do not allow ' followed by < .
  1289.  */
  1290. void prepare_insplot_parm(char *p)
  1291. {
  1292.     int i,j,multanim; char *pp, *s;
  1293.     double d;
  1294.     char setbuf[MAX_LINELEN+10],buf[MAX_LINELEN+1];
  1295.  
  1296.     j=strlen(p);
  1297. /* pipe in plot command */
  1298.     for(i=0;i<j;i++) {
  1299.       if(*(p+i)!='\'' && *(p+i)!='"') continue;
  1300.       pp=find_word_start(p+i+1); if(*pp=='<') module_error("illegal_plot_cmd");
  1301.     }
  1302.     gnuplot_patch(p,1);
  1303. /* multiplot */
  1304.     multanim=0;
  1305.     pp=getvar("insplot_split");
  1306.     if(pp!=NULL) i=linenum(pp); else i=0;
  1307. /* arbitrary limit: 16 multiplots */
  1308.     if(i>16) i=16;
  1309.     if(i>1) {
  1310.       char tbuf[MAX_LINELEN*(i+1)+100], bbuf[MAX_LINELEN+1];
  1311.       tbuf[0]=0;
  1312.       if(*p!=0) snprintf(tbuf,sizeof(tbuf),"%s\n",p);
  1313.       snprintf(buf,sizeof(buf),"%d",i); setenv("multiplot",buf,1);
  1314.       for(j=1;j<=i;j++) {
  1315.         snprintf(buf,sizeof(buf),"insplot_parm_%d",j);
  1316.         pp=getvar(buf);
  1317.         if(pp==NULL || *pp==0) {
  1318.           if(j==1 && *p!=0) continue;
  1319.           pp="";
  1320.         }
  1321.         else {
  1322.           mystrncpy(bbuf,pp,sizeof(bbuf));
  1323.           gnuplot_patch(bbuf,1);
  1324.         }
  1325.         strcat(tbuf,bbuf);strcat(tbuf,"\n");
  1326.      }
  1327.     setenv("insplot_source",tbuf,1);
  1328.     if(varchr(tbuf,"s")!=NULL) multanim=1;
  1329.     }
  1330. /* no illegal chaining */
  1331.     pp=getvar("insplot_font"); if(pp!=NULL) {
  1332.       for(s=pp;s<pp+MAX_LINELEN && *s;s++)
  1333.       if(*s==';' || *s=='\n' || *s==' ') *s=0;
  1334.       if(s>=pp+MAX_LINELEN) *s=0;
  1335.       setvar("insplot_font",pp);
  1336.     }
  1337.     pp=getvar("insplot_set"); if(pp!=NULL) {
  1338.       char tbuf[MAX_LINELEN+1];
  1339.       mystrncpy(tbuf,pp,sizeof(tbuf));
  1340.       i=strlen(tbuf)-1;
  1341.       while(i>0 && isspace(tbuf[i])) i--;
  1342.       if(tbuf[i]==';') tbuf[i]=0;
  1343.       gnuplot_patch(tbuf,0);pp=tbuf;
  1344.       ovlstrcpy(setbuf,"set "); j=strlen("set ");
  1345.       for(i=0; *(pp+i)!=0 && j<MAX_LINELEN; i++) {
  1346.         if(*(pp+i)=='\n') {setbuf[j++]=' '; continue;}
  1347.         if(*(pp+i)!=';') {setbuf[j++]=*(pp+i); continue;}
  1348.         ovlstrcpy(setbuf+j,"\nset "); j+=strlen("\nset ");
  1349.       }
  1350.       setbuf[j]=0;
  1351.       setenv("insplot_set",setbuf,1);
  1352.     }
  1353.     else setenv("insplot_set","",1);
  1354. /* frames of animation */
  1355.     pp=getvar("ins_anim_frames");
  1356.     if(pp!=NULL) i=evalue(pp); else i=1;
  1357.     if(i>=ANIM_LIMIT) i=ANIM_LIMIT-1;
  1358.     if(i<1) i=1;
  1359.     if(strstr(setbuf,"step")==NULL && strstr(p,"step")==NULL
  1360.        && varchr(setbuf,"s")==NULL && varchr(p,"s")==NULL && !multanim) i=1;
  1361.     setenv("ins_anim_frames",int2str(i),1);
  1362.     setvar("ins_anim_frames","");
  1363.     if(i>1) {setvar("ins_animation","yes");animated_ins=1;}
  1364.     else setvar("ins_animation","no");
  1365. /* delay of animation */
  1366.     pp=getvar("ins_anim_delay");
  1367.     if(pp!=NULL) d=evalue(pp); else d=0;
  1368.     if(d>=10) d=10;
  1369.     if(d<0) d=0;
  1370.     setenv("ins_anim_delay",int2str(d*100),1);
  1371. }
  1372.  
  1373. /* Insert dynamic 2d plot */
  1374. void exec_insplot(char *p)
  1375. {
  1376.     char *fmt;
  1377.     if(robot_access) {
  1378.       *p=0; return;
  1379.     }
  1380.     fmt=getvar("ins_format"); if(fmt==NULL || *fmt==0) fmt=DEFAULT_INS_FORMAT;
  1381.     prepare_insplot_parm(p); setenv("insplot_method","2D",1);
  1382.     _exec_ins(p,insplot_processor,fmt);
  1383.     wrapexec=1;
  1384. /*    call_ssh("mv %s/insplot_cmd %s 2>/dev/null",tmp_dir,s2_prefix); */
  1385.     unsetenv("multiplot"); setvar("insplot_split","");
  1386. }
  1387.  
  1388. /* Insert dynamic 3d plot */
  1389. void exec_insplot3d(char *p)
  1390. {
  1391.     char *fmt;
  1392.     if(robot_access) {
  1393.      *p=0; return;
  1394.     }
  1395.     fmt=getvar("ins_format"); if(fmt==NULL || *fmt==0) fmt=DEFAULT_INS_FORMAT;
  1396.     prepare_insplot_parm(p); setenv("insplot_method","3D",1);
  1397.     _exec_ins(p,insplot_processor,fmt);
  1398.     wrapexec=1;
  1399. /*     call_ssh("mv %s/insplot_cmd %s 2>/dev/null",tmp_dir,s2_prefix); */
  1400.     unsetenv("multiplot");setvar("insplot_split","");
  1401. }
  1402.  
  1403. /* Insert dynamic gif draw. The parm preparation is specific to fly. */
  1404. void exec_insdraw(char *p)
  1405. {
  1406.     char *pp, *fmt;
  1407.     int i;
  1408.     double d;
  1409.  
  1410.     if(robot_access) {
  1411.       *p=0; return;
  1412.     }
  1413. /*    calc_tolower(p);*/
  1414.     fmt=getvar("ins_format"); if(fmt==NULL || *fmt==0) fmt=DEFAULT_INS_FORMAT;
  1415.     while((pp=wordchr(p,"output"))!=NULL) memmove(pp,"zqkwfx",6);
  1416. /* frames of animation */
  1417.     pp=getvar("ins_anim_frames");
  1418.     if(pp!=NULL) i=evalue(pp); else i=1;
  1419.     if(i>=ANIM_LIMIT) i=ANIM_LIMIT-1;
  1420.     if(i<1) i=1;
  1421.     if(i>1 && varchr(p,"s")==NULL && varchr(p,"animstep")==NULL
  1422.        && varchr(p,"step")==NULL) i=1;
  1423.     setenv("ins_anim_frames",int2str(i),1);
  1424.     setvar("ins_anim_frames","");
  1425.     if(i>1) {setvar("ins_animation","yes");animated_ins=1;}
  1426.     else setvar("ins_animation","no");
  1427. /* delay of animation */
  1428.     pp=getvar("ins_anim_delay");
  1429.     if(pp!=NULL) d=evalue(pp); else d=0;
  1430.     if(d>=10) d=10;
  1431.     if(d<0) d=0;
  1432.     setenv("ins_anim_delay",int2str(d*100),1);
  1433.     pp=getvar("insdraw_filebase");
  1434.     if(pp!=NULL && strstr(pp,parent_dir_string)!=NULL)
  1435.       setvar("insdraw_filebase","");
  1436.     _exec_ins(p,insdraw_processor,fmt);
  1437. }
  1438.  
  1439. void exec_increase(char *p)
  1440. {
  1441.     char *p1, *p2;
  1442.     p1=find_word_start(p); p2=find_word_end(p1);
  1443.     if(p2<=p1) {
  1444.       *p=0; return;
  1445.     }
  1446.     *p2=0;p2=getvar(p1);
  1447.     if(p2==NULL) p2="";
  1448.     setvar(p1,int2str(atoi(p2)+1)); *p=0;
  1449. }
  1450.  
  1451. void exec_setseed(char *p)
  1452. {
  1453.     double d=evalue(p);
  1454.     srandom(d); *p=0;
  1455. }
  1456.  
  1457. /* bound a variable */
  1458. void exec_bound(char *p)
  1459. {
  1460.     char *p1, *p2, *p3;
  1461.     int doub,i,bcnt,defaulted;
  1462.     double d1,d2,dd,val;
  1463.     char nbuf[MAX_LINELEN+1],lbuf[MAX_LINELEN+1],dbuf[MAX_LINELEN+1];
  1464.     char vbuf[MAX_LINELEN+1];
  1465.     char *blist[2048];
  1466.  
  1467.     p1=find_word_start(p); p2=find_word_end(p1);
  1468.     if(*p2==0) {
  1469.       syntax: module_error("syntax_error");
  1470.     }
  1471.     *p2=0; ovlstrcpy(nbuf,p1);substit(nbuf); p1=find_word_start(p2+1);
  1472.     p2=getvar(nbuf);if(p2==NULL) p2="";
  1473.     mystrncpy(vbuf,find_word_start(p2),sizeof(vbuf));
  1474.     strip_trailing_spaces(vbuf);
  1475.     p2=find_word_end(p1); if(*p2==0) goto syntax;
  1476.     *p2=0;p2++;
  1477.     p3=wordchr(p2,"default");
  1478.     if(p3!=NULL) {
  1479.       *p3=0; defaulted=1;
  1480.       p3=find_word_start(p3+strlen("default"));
  1481.       ovlstrcpy(dbuf,p3); substit(dbuf);
  1482.     }
  1483.     else defaulted=0;
  1484.     if(strcmp(p1,"between")==0) {
  1485.       p1=find_word_start(p2);
  1486.       i=strlen("integer");
  1487.       if(strncmp(p1,"integer",i)==0 &&
  1488.        (isspace(*(p1+i)) || (*(p1+i)=='s' && isspace(*(p1+i+1))))) {
  1489.         doub=0; p1=find_word_start(find_word_end(p1));
  1490.         val=rint(evalue(vbuf));
  1491.         if(vbuf[0]) float2str(val,vbuf);
  1492.       }
  1493.       else {
  1494.         doub=1;val=evalue(vbuf);
  1495.       }
  1496.       p2=wordchr(p1,"and"); p3=p2+strlen("and");
  1497.       if(p2==NULL) {
  1498.        p2=strchr(p1,','); p3=p2+1;
  1499.       }
  1500.      if(p2==NULL) goto syntax;
  1501.      *p2=0;p2=find_word_start(p3);
  1502.      if(*p1==0 || *p2==0) goto syntax;
  1503.      d1=evalue(p1);d2=evalue(p2);
  1504.      if(!isfinite(d1) || !isfinite(d2) ||
  1505.        fabs(d1)>(double)(1E10) || fabs(d2)>(double)(1E10)) goto syntax;
  1506.      if(d1>d2) {
  1507.         dd=d1;d1=d2;d2=dd;
  1508.      }
  1509.      if(vbuf[0] && val<=d2 && val>=d1) {
  1510.         if(!doub) setvar(nbuf,vbuf);
  1511.         *p=0; return;
  1512.      }
  1513.      if(defaulted) ovlstrcpy(p,dbuf);
  1514.      else {
  1515.       if(!doub) {
  1516.        d1=ceil(d1);d2=floor(d2);
  1517.       }
  1518.       if(vbuf[0]==0 || val<d1) val=d1;
  1519.       else val=d2;
  1520.       float2str(val,p);
  1521.      }
  1522.      setvar(nbuf,p); *p=0; return;
  1523.     }
  1524.     else {
  1525.     if(strcmp(p1,"within")==0 || strcmp(p1,"among")==0) {
  1526.         ovlstrcpy(lbuf,p2);substit(lbuf);
  1527.         bcnt=cutitems(lbuf,blist,2048);
  1528.         if(bcnt<=0) {
  1529.           *p=0; return;
  1530.         }
  1531.         for(i=0;i<bcnt;i++) {
  1532.           if(strcmp(blist[i],vbuf)==0) {
  1533.             *p=0; return;
  1534.           }
  1535.         }
  1536.         if(defaulted) ovlstrcpy(p,dbuf); else ovlstrcpy(p,blist[0]);
  1537.         setvar(nbuf,p); *p=0;
  1538.         return;
  1539.     }
  1540.     else goto syntax;
  1541.     }
  1542. }
  1543.  
  1544. /* detrust the module. */
  1545. void exec_detrust(char *p)
  1546. {    untrust|=1; *p=0; }
  1547.  
  1548. void exec_warn(char *p)
  1549. {
  1550.     char *p1,*p2;
  1551.     char buf[MAX_FNAME+1];
  1552.     WORKING_FILE save;
  1553.  
  1554.     if(!outputing) goto end;
  1555.     p1=find_word_start(p);p2=find_word_end(p1);
  1556.     if(p2<=p1) goto end;
  1557.     *p2=0;
  1558.     snprintf(buf,sizeof(buf),"wims_warn_%s",p1);
  1559.     p2=getvar(buf);
  1560.     if(p2==NULL || *p2==0) goto end;
  1561.     p2=getvar("module_language");if(p2==NULL) p2="en";
  1562.     mkfname(buf,"msg/warn_%s.phtml.%s",p1,p2);
  1563.     memmove(&save,&m_file,sizeof(WORKING_FILE));
  1564.     if(open_working_file(&m_file,buf)==0) phtml_put(NULL,0);
  1565.     memmove(&m_file,&save,sizeof(WORKING_FILE));
  1566.     end:
  1567.     *p=0; return;
  1568. }
  1569.  
  1570. /* write an error message. */
  1571. void exec_msg(char *p)
  1572. {
  1573.     char *p1,*p2, buf[64], *l;
  1574.     secure_exec();
  1575.     p1=find_word_start(p); p2=find_word_end(p1);
  1576.     if(*p2) {
  1577.       *p2=0; p2=find_word_start(p2+1);
  1578.     }
  1579.     force_setvar("wims_error",p1); force_setvar("wims_error_parm",p2);
  1580.     l=getvar("module_language");
  1581.     if(l!=NULL && strlen(l)==2) {
  1582.       snprintf(buf,sizeof(buf),"msg.phtml.%s",l);
  1583.       phtml_put_base(buf,0);
  1584.     }
  1585.     *p=0;
  1586. }
  1587.  
  1588. struct distr_cmd distr_cmd[]={
  1589.       {"char",      NULL},
  1590.       {"charof",    NULL},
  1591.       {"chars",     NULL},
  1592.       {"charsof",   NULL},
  1593.       {"item",      cutitems},
  1594.       {"itemof",    cutitems},
  1595.       {"items",     cutitems},
  1596.       {"itemsof",   cutitems},
  1597.       {"line",      cutlines},
  1598.       {"lineof",    cutlines},
  1599.       {"lines",     cutlines},
  1600.       {"linesof",   cutlines},
  1601.       {"list",      cutitems},
  1602.       {"word",      cutwords},
  1603.       {"wordof",    cutwords},
  1604.       {"words",     cutwords},
  1605.       {"wordsof",   cutwords}
  1606. };
  1607.  
  1608. int distr_cmd_no=(sizeof(distr_cmd)/sizeof(distr_cmd[0]));
  1609.  
  1610. /* distribute a number of lines, items, etc. into a list of vars. */
  1611. void exec_distribute(char *p)
  1612. {
  1613.     int i,k,n;
  1614.     char *p1, *p2;
  1615.     char bf1[MAX_LINELEN+1],bf2[MAX_LINELEN+1];
  1616.     char *names[4096],*vals[4096];
  1617.     p1=find_word_start(p); p2=find_word_end(p1);
  1618.     if(p2<=p1 || *p2==0) module_error("syntax_error");
  1619.     *p2++=0;
  1620.     i=search_list(distr_cmd,distr_cmd_no,sizeof(distr_cmd[0]),p1);
  1621.     if(i<0) module_error("syntax_error");
  1622.     p2=find_word_start(p2); p1=wordchr(p2,"into");
  1623.     if(p1==NULL) module_error("syntax_error");
  1624.     *p1=0;mystrncpy(bf1,p2,sizeof(bf1));
  1625.     p1=find_word_start(p1+strlen("into"));
  1626.     mystrncpy(bf2,p1,sizeof(bf2));
  1627.     substit(bf1);substit(bf2);
  1628.     strip_trailing_spaces(bf1);
  1629.     items2words(bf2); n=cutwords(bf2,names,4096);
  1630.     if(distr_cmd[i].routine!=NULL) {
  1631.       k=distr_cmd[i].routine(bf1,vals,n);
  1632.       for(i=0;i<k;i++) setvar(names[i],vals[i]);
  1633.       for(;i<n;i++) setvar(names[i],"");
  1634.     }
  1635.     else {
  1636.       char buf[2];
  1637.       buf[1]=0;
  1638.       for(p1=bf1,i=0;i<n;i++) {
  1639.         buf[0]=*p1; if(*p1) p1++;
  1640.         setvar(names[i],buf);
  1641.       }
  1642.     }
  1643. }
  1644.  
  1645. /* reset variables */
  1646. void exec_reset(char *p)
  1647. {
  1648.     char *p1, *p2;
  1649.  
  1650.     items2words(p);
  1651.     for(p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
  1652.       p2=find_word_end(p1); if(*p2) *p2++=0;
  1653.       setvar(p1,"");
  1654.     }
  1655. }
  1656.  
  1657. /* exchange the values of two variables */
  1658. void exec_exchange(char *p)
  1659. {
  1660.     char buf[MAX_LINELEN+1],b1[MAX_LINELEN+1],b2[MAX_LINELEN+1];
  1661.     char *p1,*p2,*pb;
  1662.     p1=wordchr(p,"and");
  1663.     if(p1!=NULL) {
  1664.       *p1=0; p2=find_word_start(p1+strlen("and"));
  1665.     }
  1666.     else {
  1667.       p1=strchr(p,',');
  1668.       if(p1==NULL) module_error("syntax_error");
  1669.       *p1=0; p2=find_word_start(p1+1);
  1670.     }
  1671.     p1=find_word_start(p);
  1672.     mystrncpy(b1,p1,sizeof(b1)); substit(b1); *find_word_end(b1)=0;
  1673.     mystrncpy(b2,p2,sizeof(b2)); substit(b2); *find_word_end(b2)=0;
  1674.     if(*b1==0 || *b2==0) module_error("syntax_error");
  1675.     pb=getvar(b1);if(pb==NULL) pb="";
  1676.     mystrncpy(buf,pb,sizeof(buf));
  1677.     pb=getvar(b2);if(pb==NULL) pb="";
  1678.     setvar(b1,pb); setvar(b2,buf);
  1679. }
  1680.  
  1681. /* Send a mail */
  1682. void exec_mailto(char *p)
  1683. {
  1684.     char *p1,*p2,*pp;
  1685.  
  1686.     if(!trusted_module() || is_class_module) return;
  1687.     p1=strchr(p,'\n'); if(p1==NULL) return;
  1688.     *p1++=0; p=find_word_start(p);
  1689.     if(*p==0) return;
  1690.     p2=strchr(p1,'\n'); if(p2==NULL) return;
  1691.     *p2++=0;
  1692.     for(pp=p1;*pp;pp++) if(*pp=='"' || *pp=='\n') *pp=' ';
  1693.     accessfile(p2,"w","%s/mail.body",tmp_dir);
  1694.     wrapexec=1;
  1695.     call_sh("mail %s -s \" %s \" %s <%s/mail.body; chmod og-rwx %s/mail.body",
  1696.         mail_opt, p1,p,tmp_dir,tmp_dir);
  1697.     mail_log(p);
  1698.     *p=0;
  1699. }
  1700.  
  1701. /* Generates a user error. Internal and undocumented. */
  1702. void exec_usererror(char *p)
  1703. {
  1704.     if(trusted_module()) user_error(p);
  1705. }
  1706.  
  1707. /* stop output. */
  1708. void exec_directout(char *p)
  1709. {
  1710.     if(outputing || !trusted_module()) return;
  1711.     printf("%s",p);
  1712.     noout=1;
  1713. }
  1714.  
  1715. enum {
  1716.       EXEC_IF, EXEC_JUMP, EXEC_ELSE, EXEC_ENDIF, EXEC_EXEC,
  1717.       EXEC_WHILE, EXEC_ENDWHILE,
  1718.       EXEC_FOR, EXEC_VAR, EXEC_DEBUG, EXEC_DAEMON,
  1719.       EXEC_SET, EXEC_DEFAULT, EXEC_COMMENT, EXEC_READ, EXEC_HREF,
  1720.       EXEC_INS, EXEC_STRING, EXEC_PEDIA, EXEC_DIR,
  1721.       EXEC_TRUST, EXEC_WARN, EXEC_ERROR, EXEC_SQL, EXEC_SCORE,
  1722.       EXEC_MAIL, EXEC_OTHER
  1723. } EXEC_TYPES;
  1724. #define EXEC_SUBST 0x1000
  1725. #define EXEC_USECALC 0x2000
  1726. #define EXEC_PROCTOO 0x4000
  1727. MYFUNCTION exec_routine[]={
  1728.       {"!",        EXEC_COMMENT,        exec_comment},
  1729.       {"TeXmath",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,texmath},
  1730.       {"add",      EXEC_STRING|EXEC_USECALC,calc_sum},
  1731.       {"advance",  EXEC_VAR|EXEC_SUBST,   exec_increase},
  1732.       {"append",   EXEC_STRING|EXEC_USECALC,calc_append},
  1733.       {"appendfile",EXEC_DIR|EXEC_SUBST,    fileappend},
  1734.       {"bound",     EXEC_STRING,       exec_bound},
  1735.       {"break",     EXEC_FOR,        exec_break},
  1736.       {"call",      EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
  1737.       {"changeto",  EXEC_READ|EXEC_SUBST,    exec_changeto},
  1738.       {"char",      EXEC_STRING|EXEC_USECALC,calc_charof},
  1739.       {"chars",     EXEC_STRING|EXEC_USECALC,calc_charof},
  1740.       {"checkhost", EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_checkhost},
  1741.       {"column",    EXEC_STRING|EXEC_USECALC,calc_columnof},
  1742.       {"columns",   EXEC_STRING|EXEC_USECALC,calc_columnof},
  1743.       {"comment",   EXEC_COMMENT,        exec_comment},
  1744.       {"daemon",    EXEC_DAEMON|EXEC_USECALC|EXEC_SUBST,calc_daemon},
  1745.       {"date",      EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_date},
  1746.       {"deaccent",  EXEC_STRING|EXEC_USECALC|EXEC_SUBST,deaccent},
  1747.       {"debug",     EXEC_DEBUG,        calc_debug},
  1748.       {"declosing", EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_declosing},
  1749.       {"def",       EXEC_SET,        exec_set},
  1750.       {"default",   EXEC_SET,        exec_default},
  1751.       {"define",    EXEC_SET,        exec_set},
  1752.       {"definitionof", EXEC_SCORE|EXEC_USECALC,calc_defof},
  1753.       {"defof",     EXEC_SCORE|EXEC_USECALC,calc_defof},
  1754.       {"defread",   EXEC_READ|EXEC_SUBST,    exec_defread},
  1755.       {"detag",     EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_detag},
  1756.       {"detrust",   EXEC_TRUST,        exec_detrust},
  1757. /*      {"dictionary",EXEC_STRING|EXEC_USECALC,calc_dictionary},    */
  1758.       {"dir",       EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
  1759.       {"distribute",EXEC_STRING,        exec_distribute},
  1760.       {"distrust",  EXEC_TRUST,        exec_detrust},
  1761.       {"else",      EXEC_ELSE,        exec_else},
  1762.       {"embraced",  EXEC_STRING|EXEC_USECALC,calc_embraced},
  1763.       {"encyclo",   EXEC_PEDIA|EXEC_SUBST|EXEC_USECALC,pedia},
  1764.       {"encyclopedia",EXEC_PEDIA|EXEC_SUBST|EXEC_USECALC,pedia},
  1765.       {"endif",     EXEC_ENDIF,        exec_endif},
  1766.       {"endwhile",  EXEC_ENDWHILE,        exec_endwhile},
  1767.       {"evalsubst", EXEC_STRING|EXEC_USECALC,calc_evalsubst},
  1768.       {"evalsubstit",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
  1769.       {"evalsubstitute",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
  1770.       {"evaluesubst",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
  1771.       {"evaluesubstit",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
  1772.       {"evaluesubstitute",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
  1773.       {"examscore", EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_examscore},
  1774.       {"exchange",  EXEC_STRING,        exec_exchange},
  1775.       {"exec",      EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
  1776.       {"execute",   EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
  1777.       {"exit",      EXEC_JUMP,        exec_exit},
  1778.       {"fileappend",EXEC_DIR|EXEC_SUBST,    fileappend},
  1779.       {"filelist",  EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
  1780.       {"fileout",   EXEC_HREF|EXEC_SUBST,    exec_getfile},
  1781.       {"filewrite", EXEC_DIR|EXEC_SUBST,    filewrite},
  1782.       {"for",       EXEC_FOR,        exec_for},
  1783.       {"form",      EXEC_HREF|EXEC_SUBST,    exec_form},
  1784.       {"formbar",   EXEC_HREF,        exec_formbar},
  1785.       {"formcheckbox",EXEC_HREF,        exec_formcheckbox},
  1786.       {"formend",   EXEC_STRING,    exec_formend},
  1787.       {"formradio", EXEC_HREF,        exec_formradio},
  1788.       {"formradiobar",EXEC_HREF,        exec_formbar},
  1789.       {"formselect",EXEC_HREF,        exec_formselect},
  1790.       {"getdef",    EXEC_SCORE|EXEC_USECALC,calc_defof},
  1791.       {"getfile",   EXEC_HREF|EXEC_SUBST,    exec_getfile},
  1792.       {"getscore",  EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscore},
  1793.       {"getscorebest",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorebest},
  1794.       {"getscorelast",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorelast},
  1795.       {"getscorelevel",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorelevel},
  1796.       {"getscoremean",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoremean},
  1797.       {"getscorepercent",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorepercent},
  1798.       {"getscorequality",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoremean},
  1799.       {"getscoreremain",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoreremain},
  1800.       {"getscorerequire",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorerequire},
  1801.       {"getscoretry",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoretry},
  1802.       {"getscoreweight",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoreweight},
  1803.       {"goto",      EXEC_JUMP|EXEC_SUBST,    exec_goto},
  1804.       {"header",    EXEC_HREF,        exec_header},
  1805.       {"header1",   EXEC_HREF,        exec_header1},
  1806.       {"headmenu",  EXEC_HREF,        exec_headmenu},
  1807.       {"hex",       EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_hex},
  1808.       {"homeref",   EXEC_HREF,        exec_homeref},
  1809.       {"href",      EXEC_HREF,        exec_href},
  1810.       {"htmlbar",   EXEC_HREF,        exec_formbar},
  1811.       {"htmlcheckbox",EXEC_HREF,        exec_formcheckbox},
  1812.       {"htmlheader",EXEC_HREF,        exec_header},
  1813.       {"htmlmath",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,htmlmath},
  1814.       {"htmlradio", EXEC_HREF,        exec_formradio},
  1815.       {"htmlradiobar",EXEC_HREF,        exec_formbar},
  1816.       {"htmlselect",EXEC_HREF,        exec_formselect},
  1817.       {"htmltail",  EXEC_HREF,        exec_tail},
  1818.       {"htmltitle", EXEC_HREF,        exec_title},
  1819.       {"if",        EXEC_IF,        exec_if},
  1820.       {"ifval",     EXEC_IF,        exec_ifval},
  1821.       {"ifvalue",   EXEC_IF,        exec_ifval},
  1822.       {"imgrename", EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_imgrename},
  1823.       {"include",   EXEC_READ|EXEC_SUBST,   exec_read},
  1824.       {"increase",  EXEC_VAR|EXEC_SUBST,    exec_increase},
  1825.       {"input",     EXEC_READ|EXEC_SUBST,    exec_read},
  1826.       {"insdraw",   EXEC_INS|EXEC_SUBST,    exec_insdraw},
  1827.       {"insmath",   EXEC_INS,        insmath},
  1828.       {"inspaint",  EXEC_INS|EXEC_SUBST,    exec_insdraw},
  1829.       {"insplot",   EXEC_INS|EXEC_SUBST,    exec_insplot},
  1830.       {"insplot3d", EXEC_INS|EXEC_SUBST,    exec_insplot3d},
  1831.       {"instex",    EXEC_INS,        exec_instex},
  1832.       {"instexst",  EXEC_INS|EXEC_USECALC,    calc_instexst},
  1833.       {"instexstatic",EXEC_INS|EXEC_USECALC,    calc_instexst},
  1834.       {"item",        EXEC_STRING|EXEC_USECALC,calc_itemof},
  1835.       {"items",     EXEC_STRING|EXEC_USECALC,calc_itemof},
  1836.       {"items2lines", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
  1837.       {"items2words",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
  1838.       {"itemstolines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
  1839.       {"itemstowords",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
  1840.       {"let",        EXEC_SET,        exec_set},
  1841.       {"leveldata",  EXEC_STRING|EXEC_USECALC,calc_leveldata},
  1842.       {"levelpoints",EXEC_STRING|EXEC_USECALC,calc_leveldata},
  1843.       {"line",       EXEC_STRING|EXEC_USECALC,calc_lineof},
  1844.       {"lines",      EXEC_STRING|EXEC_USECALC,calc_lineof},
  1845.       {"lines2items",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
  1846.       {"lines2list", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
  1847.       {"lines2words",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2words},
  1848.       {"linestoitems",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
  1849.       {"linestolist",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
  1850.       {"linestowords",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2words},
  1851.       {"list2lines", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
  1852.       {"list2words", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
  1853.       {"listfile",   EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
  1854.       {"listfiles",  EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
  1855.       {"listintersect",EXEC_STRING|EXEC_USECALC,calc_listintersect},
  1856.       {"listintersection", EXEC_STRING|EXEC_USECALC,calc_listintersect},
  1857.       {"listtolines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
  1858.       {"listtowords",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
  1859.       {"listunion",  EXEC_STRING|EXEC_USECALC,calc_listunion},
  1860.       {"listuniq",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_listuniq},
  1861.       {"listunique", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_listuniq},
  1862.       {"listvar",    EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathvarlist},
  1863.       {"lookup",     EXEC_STRING|EXEC_USECALC,    calc_lookup},
  1864.       {"lower",      EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_tolower},
  1865.       {"lowercase",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_tolower},
  1866.       {"ls",         EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
  1867.       {"mailto",     EXEC_MAIL|EXEC_SUBST,    exec_mailto},
  1868.       {"mailurl",    EXEC_MAIL|EXEC_SUBST|EXEC_USECALC,calc_mailurl},
  1869.       {"makelist",   EXEC_STRING|EXEC_USECALC,calc_makelist},
  1870.       {"math2html",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,htmlmath},
  1871.       {"math2mathml",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathmlmath},
  1872.       {"math2tex",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,texmath},
  1873.       {"mathmlmath", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathmlmath},
  1874.       {"mathsubst",  EXEC_STRING|EXEC_USECALC,calc_mathsubst},
  1875.       {"mathsubstit",EXEC_STRING|EXEC_USECALC,calc_mathsubst},
  1876.       {"mathsubstitute",EXEC_STRING|EXEC_USECALC,calc_mathsubst},
  1877.       {"mexec",     EXEC_EXEC|EXEC_SUBST,    exec_mexec},
  1878.       {"module",    EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_module},
  1879.       {"msg",       EXEC_EXEC|EXEC_SUBST,exec_msg},
  1880.       {"multiply",  EXEC_STRING|EXEC_USECALC,calc_product},
  1881.       {"next",      EXEC_FOR,        exec_next},
  1882.       {"nocache",   EXEC_READ,        exec_nocache},
  1883.       {"non_empty", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_nonempty},
  1884.       {"nonempty",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_nonempty},
  1885.       {"nospace",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,nospace},
  1886.       {"outfile",   EXEC_HREF|EXEC_SUBST,    exec_getfile},
  1887.       {"pedia",     EXEC_PEDIA|EXEC_SUBST|EXEC_USECALC,pedia},
  1888.       {"perl",      EXEC_EXEC|EXEC_PROCTOO|EXEC_SUBST,exec_perl},
  1889.       {"position",  EXEC_STRING|EXEC_USECALC,calc_pos},
  1890.       {"positionof",EXEC_STRING|EXEC_USECALC,calc_pos},
  1891.       {"positions", EXEC_STRING|EXEC_USECALC,calc_pos},
  1892.       {"prod",      EXEC_STRING|EXEC_USECALC,calc_product},
  1893.       {"product",   EXEC_STRING|EXEC_USECALC,calc_product},
  1894.       {"rawmath",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,rawmath},
  1895.       {"rawmatrix", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,rawmatrix},
  1896.       {"reaccent",  EXEC_STRING|EXEC_USECALC|EXEC_SUBST,reaccent},
  1897.       {"read",      EXEC_READ|EXEC_SUBST,    exec_read},
  1898.       {"readdef",   EXEC_READ|EXEC_SUBST,    exec_defread},
  1899.       {"readproc",  EXEC_READ|EXEC_SUBST,    exec_readproc},
  1900.       {"record",    EXEC_STRING|EXEC_USECALC,calc_recordof},
  1901.       {"records",   EXEC_STRING|EXEC_USECALC,calc_recordof},
  1902.       {"recursion", EXEC_STRING|EXEC_USECALC,calc_recursion},
  1903.       {"reinput",   EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_reinput},
  1904.       {"rem",       EXEC_COMMENT,        exec_comment},
  1905.       {"remark",    EXEC_COMMENT,        exec_comment},
  1906.       {"replace",   EXEC_STRING|EXEC_USECALC,calc_replace},
  1907.       {"reset",     EXEC_SET|EXEC_SUBST,    exec_reset},
  1908.       {"restart",   EXEC_JUMP|EXEC_SUBST,    exec_restart},
  1909.       {"return",    EXEC_JUMP,        exec_exit},
  1910.       {"robotrap",  EXEC_HREF|EXEC_SUBST,    exec_robottrap},
  1911.       {"robottrap", EXEC_HREF|EXEC_SUBST,    exec_robottrap},
  1912.       {"rootof",    EXEC_STRING|EXEC_USECALC,calc_solve},
  1913.       {"row",       EXEC_STRING|EXEC_USECALC,calc_rowof},
  1914.       {"rows",      EXEC_STRING|EXEC_USECALC,calc_rowof},
  1915.       {"rows2lines",EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_rows2lines},
  1916.       {"run",       EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
  1917.       {"select",    EXEC_STRING|EXEC_USECALC,calc_select},
  1918.       {"set",       EXEC_SET,        exec_set},
  1919.       {"setdef",    EXEC_OTHER,        exec_setdef},
  1920.       {"setseed",   EXEC_STRING|EXEC_SUBST,        exec_setseed},
  1921.       {"sh",        EXEC_EXEC|EXEC_PROCTOO|EXEC_SUBST,exec_sh},
  1922.       {"shortout",  EXEC_JUMP|EXEC_SUBST,    exec_directout},
  1923.       {"singlespace",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,singlespace},
  1924.       {"solve",     EXEC_STRING|EXEC_USECALC,calc_solve},
  1925.       {"sort",      EXEC_STRING|EXEC_USECALC, calc_sort},
  1926. /*      {"sql",     EXEC_SQL|EXEC_SUBST|EXEC_USECALC, calc_sql}, */
  1927.       {"staticinstex",EXEC_INS|EXEC_USECALC,    calc_instexst},
  1928.       {"stinstex",  EXEC_INS|EXEC_USECALC,    calc_instexst},
  1929.       {"sum",       EXEC_STRING|EXEC_USECALC,calc_sum},
  1930.       {"system",    EXEC_EXEC|EXEC_PROCTOO|EXEC_SUBST,exec_sh},
  1931.       {"tail",      EXEC_HREF,        exec_tail},
  1932.       {"test",      EXEC_DEBUG,        exec_test},
  1933.       {"texmath",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,texmath},
  1934.       {"text",      EXEC_STRING|EXEC_USECALC,text},
  1935.       {"title",     EXEC_HREF,        exec_title},
  1936.       {"tohex",     EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_hex},
  1937.       {"tolower",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_tolower},
  1938.       {"toupper",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_toupper},
  1939.       {"translate", EXEC_STRING|EXEC_USECALC,calc_translate},
  1940.       {"trim",      EXEC_STRING|EXEC_USECALC,calc_trim},
  1941.       {"upper",     EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_toupper},
  1942.       {"uppercase", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_toupper},
  1943.       {"usererror", EXEC_WARN|EXEC_SUBST,    exec_usererror},
  1944.       {"values",    EXEC_STRING|EXEC_USECALC,calc_values},
  1945.       {"varlist",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathvarlist},
  1946.       {"warn",      EXEC_WARN|EXEC_SUBST,    exec_warn},
  1947.       {"warning",   EXEC_WARN|EXEC_SUBST,    exec_warn},
  1948.       {"while",     EXEC_WHILE,        exec_while},
  1949.       {"whileval",  EXEC_WHILE,        exec_whileval},
  1950.       {"whilevalue",EXEC_WHILE,        exec_whileval},
  1951.       {"wimsheader",EXEC_HREF,        exec_header},
  1952.       {"wimsref",   EXEC_HREF,        exec_homeref},
  1953.       {"wimstail",  EXEC_HREF,        exec_tail},
  1954.       {"wimstitle", EXEC_HREF,        exec_title},
  1955.       {"word",      EXEC_STRING|EXEC_USECALC,calc_wordof},
  1956.       {"words",     EXEC_STRING|EXEC_USECALC,calc_wordof},
  1957.       {"words2items",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
  1958.       {"words2lines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2lines},
  1959.       {"words2list",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
  1960.       {"wordstoitems",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
  1961.       {"wordstolines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2lines},
  1962.       {"wordstolist",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
  1963.       {"writefile", EXEC_DIR|EXEC_SUBST,    filewrite},
  1964. };
  1965. int EXEC_FN_NO=(sizeof(exec_routine)/sizeof(exec_routine[0]));
  1966.  
  1967. /* internal: to skip the content of a false if/while. */
  1968. static void _skip_contents(int isif)
  1969. {
  1970.     char buf[MAX_NAMELEN+8], *p1;
  1971.     int i,j,loop;
  1972.     loop=0;
  1973.     while(m_file.linepointer<m_file.linecnt) {
  1974.       j=m_file.linepointer;
  1975.       if((m_file.lines[j].isstart&2)==0) {
  1976.         m_file.linepointer++; continue;
  1977.       }
  1978.       i=m_file.lines[j].execcode;
  1979.       if(i<0) {
  1980.         if(wgetline(buf,MAX_NAMELEN+4,&m_file)==EOF) return;
  1981.         p1=buf+1; if(*p1!='i' && *p1!='e' && *p1!='w') continue;
  1982.         *find_word_end(p1)=0;
  1983.         i=search_list(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]),p1);
  1984.         if(i>=0) m_file.lines[j].execcode=i;
  1985.       }
  1986.       else m_file.linepointer++;
  1987.       if(i<0) continue;
  1988.       switch(exec_routine[i].tag & 0xffff) {
  1989.         case EXEC_WHILE:
  1990.           if(!isif) loop++;
  1991.           break;
  1992.         case EXEC_IF:
  1993.           if(isif) loop++;
  1994.           break;
  1995.         case EXEC_ELSE: {
  1996.           if(!isif) break;
  1997.           if(loop<=0) return; else break;
  1998.         }
  1999.         case EXEC_ENDIF: {
  2000.           if(!isif) break;
  2001.           if(loop>0) {
  2002.             loop--; break;
  2003.           }
  2004.          else return;
  2005.         }
  2006.         case EXEC_ENDWHILE: {
  2007.           if(isif) break;
  2008.           if(loop>0) {
  2009.               loop--; break;
  2010.           }
  2011.           else return;
  2012.         }
  2013.         default: break;
  2014.       }
  2015.     }
  2016. }
  2017.  
  2018. /* Execute a command defined by !. Returns 0 if OK. */
  2019. void exec_main(char *p)
  2020. {
  2021.     int i,j;
  2022.     char *pp;
  2023.     char tbuf2[MAX_LINELEN+1];
  2024.  
  2025.     pp=find_word_end(p);
  2026.     if(*pp!=0) {
  2027.       *(pp++)=0; pp=find_word_start(pp);
  2028.     }
  2029.     i=m_file.lines[m_file.l].execcode;
  2030.     if(i<0) {
  2031.       i=search_list(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]),p);
  2032.       m_file.lines[m_file.l].execcode=i;
  2033.     }
  2034.     if(i<0) {
  2035.       setvar(error_data_string,p); module_error("bad_cmd");
  2036.     }
  2037. /* called from !readdef, no right other than set; bail out */
  2038.     execnt++;
  2039.     if((untrust&4)!=0 && (j=(exec_routine[i].tag&0xffff))!=EXEC_SET) {
  2040.       tbuf2[0]=0; exec_exit(tbuf2);
  2041.     }
  2042.     ovlstrcpy(tbuf2,pp); j=exec_routine[i].tag;
  2043.     if(j&EXEC_SUBST) substit(tbuf2);
  2044.     if(j&EXEC_USECALC) {
  2045.       if(!outputing && (j&EXEC_PROCTOO)==0) return;
  2046.       exec_routine[i].routine(tbuf2); if(outputing) output0(tbuf2);
  2047.     }
  2048.     else exec_routine[i].routine(tbuf2);
  2049.     return;
  2050. }
  2051.  
  2052.