Subversion Repositories wimsdev

Rev

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