Subversion Repositories wimsdev

Rev

Rev 13757 | Rev 14664 | 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 source.
  19.  * This file does computations and output.
  20.  */
  21. #include "wims.h"
  22.  
  23. int _sort_numeric, _sort_nocase;
  24. typedef struct SORT_STRUCT {char *str; double val; int serial;} SORT_STRUCT;
  25. struct SORT_STRUCT sort_sep[MAX_SORT_ITEM+1];
  26.  
  27. void secure_exec(void)
  28. {
  29.   if((untrust&6)==0) return;
  30.   module_error("Illegal_command");
  31. }
  32.  
  33. /* internal comparers */
  34. int __sorter(const void *p1, const void *p2)
  35. {
  36.   struct SORT_STRUCT *pp1, *pp2;
  37.  
  38.   pp1=(struct SORT_STRUCT *) p1; pp2=(struct SORT_STRUCT *) p2;
  39.   if(_sort_numeric) {
  40.     double dd=(pp1->val)-(pp2->val);
  41.     if(dd>0) return 1;
  42.     if(dd<0) return -1;
  43.     return 0;
  44.   }
  45.   if(_sort_nocase) return strcasecmp(pp1->str,pp2->str);
  46.   else return strcmp(pp1->str,pp2->str);
  47. }
  48.  
  49. int _char_sorter(const void *c1, const void *c2)
  50. {
  51.   if(_sort_nocase) return tolower(*(char *)c1)-tolower(*(char *)c2);
  52.   else return *(char *)c1-*(char *)c2;
  53. }
  54.  
  55. enum {sort_char, sort_word, sort_item, sort_line, sort_row,
  56.       sort_numeric, sort_nocase, sort_reverse
  57. };
  58. struct {char *name; int type;
  59. } sort_keyw[]={
  60.   {"char",sort_char},
  61.   {"chars",sort_char},
  62.   {"character",sort_char},
  63.   {"characters",sort_char},
  64.   {"word",sort_word},
  65.   {"words",sort_word},
  66.   {"item",sort_item},
  67.   {"items",sort_item},
  68.   {"line",sort_line},
  69.   {"lines",sort_line},
  70.   {"list",sort_item},
  71.   {"numeric",sort_numeric},
  72.   {"numerical",sort_numeric},
  73.   {"nocase",sort_nocase},
  74.   {"ignorecase",sort_nocase},
  75.   {"reverse",sort_reverse},
  76.   {"row",sort_row},
  77.   {"rows",sort_row}
  78. };
  79. #define sort_keyw_no (sizeof(sort_keyw)/sizeof(sort_keyw[0]))
  80.  
  81. /* string sort */
  82. void calc_sort(char *p)
  83. {
  84.   char *p1, *p2, buf[MAX_LINELEN+1], *csep[MAX_SORT_ITEM+1];
  85.   char fs=0;
  86.   int (*cutfn)(char *p,char *list[],int max);
  87.   int nocase=0, reverse=0, numeric=0, type;
  88.   int i,j,l,t,total;
  89.  
  90.   cutfn=NULL;
  91.   for(i=0,p1=find_word_start(p);i>=0;p1=find_word_start(p2)) {
  92.     p2=find_word_end(p1);
  93.     if(*p2!=0) *p2++=0;
  94.     for(i=0;i<sort_keyw_no && strcasecmp(p1,sort_keyw[i].name)!=0;i++);
  95.     if(i>=sort_keyw_no) module_error("syntax_error");
  96.     switch(type=sort_keyw[i].type) {
  97.       case sort_nocase: nocase=1; break;
  98.       case sort_reverse: reverse=1; break;
  99.       case sort_numeric: numeric=1; break;
  100.       case sort_char: i=-1; break;
  101.       case sort_word: cutfn=cutwords; fs=' '; i=-1; break;
  102.       case sort_item: cutfn=cutitems; fs=','; i=-1; break;
  103.       case sort_row:
  104.       case sort_line: cutfn=cutlines; fs='\n'; i=-1; break;
  105.     }
  106.   }
  107.   if(p1[0]=='o' && p1[1]=='f' && myisspace(p1[2])) p1=find_word_start(p1+2);
  108.   mystrncpy(buf,p1,sizeof(buf)); substit(buf); *p=0;
  109.   t=0; if(type==sort_row) t=rows2lines(buf);
  110.   _sort_nocase=nocase; _sort_numeric=numeric;
  111.   if(cutfn) {
  112.     char ordbuf[MAX_LINELEN+1];
  113.     total=cutfn(buf,csep,MAX_SORT_ITEM+1);
  114.     if(total<=0 || total>MAX_SORT_ITEM) return;
  115.     for(i=0;i<total;i++) {
  116.       sort_sep[i].str=csep[i];
  117.       if(numeric) sort_sep[i].val=evalue(csep[i]);
  118.     }
  119.     for(i=0;i<total;i++) sort_sep[i].serial=i+1;
  120.     qsort(sort_sep,total,sizeof(sort_sep[0]),__sorter);
  121.     p1=p;p2=ordbuf;ordbuf[0]=0;
  122.     for(i=0;i<total;i++) {
  123.       if(reverse) j=total-1-i; else j=i;
  124.       l=strlen(sort_sep[j].str);
  125.       if(p1-p+l>=MAX_LINELEN-1) module_error("cmd_output_too_long");
  126.       if(p1>p) *p1++=fs;
  127.          memmove(p1,sort_sep[j].str,l+1); p1+=l;
  128.       if(i>0) *p2++=',';
  129.       mystrncpy(p2,int2str(sort_sep[j].serial),
  130.              MAX_LINELEN-(p2-ordbuf)-1);
  131.       p2+=strlen(p2);
  132.     }
  133.     force_setvar("wims_sort_order",ordbuf);
  134.   }
  135.   else { /* case of chars */
  136.     qsort(buf,strlen(buf),1,_char_sorter);
  137.     total=strlen(buf);
  138.     if(reverse) {
  139.       for(i=total-1;i>=0;i--) *(p+total-i-1)=buf[i];
  140.       *(p+total)=0;
  141.     }
  142.     else memmove(p,buf,total+1);
  143.   }
  144.   if(t) lines2rows(p);
  145. }
  146.  
  147. /* Print a debug output */
  148. void calc_debug(char *p)
  149. {
  150.   secure_exec(); setvar("debug",p); module_error("debug");
  151. }
  152.  
  153. /* execute a command thru wimslogd */
  154. void calc_daemon(char *p)
  155. {
  156.   if(!trusted_module() || is_class_module) {*p=0; return;}
  157.   _daemoncmd(p);
  158. }
  159.  
  160. void _get_exec_error(char *errorfname, char *cmdname)
  161. {
  162.   char *p2;
  163.   char obuf[MAX_LINELEN+1];
  164.   int l;
  165.   if(errorfname) accessfile(obuf,"e","%s",errorfname);
  166.   else {
  167.     l=read(mxtab[multiexec_index].pipe_stderr[0],obuf,MAX_LINELEN/4);
  168.     if(l<0 || l>MAX_LINELEN) l=0;
  169.     obuf[l]=0;
  170.   }
  171.   if(obuf[0]) {
  172.     p2=find_word_end(obuf);
  173.     if((manageable<1 || strcasecmp(tmp_debug,"yes")!=0) &&
  174.        !trusted_module() &&
  175.         strncmp(p2," not_INStalled",strlen(" not_INStalled"))==0) {
  176.       *p2=0; setvar("missing_software",obuf);
  177.       snprintf(p2+1,MAX_LINELEN-strlen(obuf)-8,"missing_%s",obuf);
  178.       if(!outputing) user_error(p2+1);
  179.     }
  180.     p2=getvar("wims_exec_error"); if(p2==NULL) p2="";
  181.     snprintf(tmplbuf,sizeof(tmplbuf),"%s\nERROR from %s:\n%s",p2,cmdname,obuf);
  182.     force_setvar("wims_exec_error",tmplbuf);
  183.   }
  184. }
  185.  
  186. /* execute an external program
  187.  * The output of the external program should be put into
  188.  * a file session_directory/session/cmd.tmp.
  189.  */
  190. void calc_exec(char *p)
  191. {
  192.   int i,j,k;
  193.   char *cmd, *parm, *pp;
  194.   char namebuf[MAX_EXEC_NAME+1], cmdstr[MAX_LINELEN+256], obuf[MAX_LINELEN+1];
  195.   char outfname[MAX_FNAME+1], errorfname[MAX_FNAME+1],
  196.     typefname[MAX_FNAME+1], varfname[MAX_FNAME+1];
  197.   char *abuf[2]={NULL,NULL};
  198.   WORKING_FILE wf;
  199.   if(robot_access || time(0)>=limtimex-1) {
  200.     *p=0;return;
  201.   }
  202.   cmd=find_word_start(p); if(*cmd==0) return; /* No command. */
  203.   parm=find_word_end(cmd);j=parm-cmd;parm=find_word_start(parm);
  204.   if(j>MAX_EXEC_NAME) {
  205.     wrong_command:
  206.     setvar(error_data_string,namebuf); module_error("bad_cmd");
  207.     *p=0; return; /* should not occur */
  208.   }
  209.   memmove(namebuf,cmd,j); namebuf[j]=0;
  210. /* Specifying parent directory in command name is of course
  211.  * prohibited.
  212.  * The module developper cannot start from root, for bin_dir
  213.  * will be prefixed to cmd.
  214.  */
  215.   if(strstr(namebuf,parent_dir_string)!=NULL) {
  216.     setvar(error_data_string,namebuf); module_error("illegal_cmd");
  217.     *p=0; return;
  218.   }
  219.   snprintf(cmdstr,sizeof(cmdstr),"%s/%s",bin_dir,namebuf);
  220.   if(ftest(cmdstr)!=is_exec) goto wrong_command;
  221.   abuf[0]=cmdstr;
  222.   mkfname(outfname,"%s/exec.out",tmp_dir);
  223.   mkfname(errorfname,"%s/exec.err",tmp_dir);
  224.   mkfname(typefname,"%s/exec.type",tmp_dir);
  225.   unlink(typefname);
  226.   mkfname(varfname,"%s/exec.var",tmp_dir);
  227.   unlink(varfname);
  228.   if(!trusted_module()) setenv("untrust","yes",1); else unsetenv("untrust");
  229.   if(strcmp(parm,"about")!=0 && multiexec(namebuf,abuf)) {
  230.     fd_set rset;
  231.     struct timeval t;
  232.     int v1, v2, fd;
  233.  
  234.     i=strlen(parm);
  235.     fd=mxtab[multiexec_index].pipe_stdin[1];
  236.     (void)write(fd,parm,i);
  237.     (void)write(fd,multiexec_random,strlen(multiexec_random));
  238.     *p=0; i=0; k=MAX_LINELEN; fd=mxtab[multiexec_index].pipe_stdout[0];
  239.     while(k>0) {
  240.       FD_ZERO(&rset); FD_SET(fd,&rset);
  241.       t.tv_sec=0; t.tv_usec=100000; v1=0; v2=300; /* 3 seconds */
  242.       if(select(fd+1,&rset,NULL,NULL,&t)<=0) {
  243.         v1++; if(v1>=v2) break;
  244.       else continue;
  245.       }
  246.       j=read(fd,p+i,k);
  247.       if(j<0 || j>k) break;
  248.       i+=j; k-=j; p[i]=0;
  249.       pp=strstr(p,multiexec_random);
  250.       if(pp) {*pp=0; break;}
  251.     }
  252.     fd=mxtab[multiexec_index].pipe_stderr[0];
  253.     FD_ZERO(&rset); FD_SET(fd,&rset);
  254.     t.tv_sec=0; t.tv_usec=0;
  255.     if(select(fd+1,&rset,NULL,NULL,&t)>0)
  256.       _get_exec_error(NULL,namebuf);
  257.     strip_trailing_spaces(p);
  258.     return;
  259.   }
  260.   exportall(); setenv("wims_exec_parm",parm,1);
  261.   execredirected(cmdstr,NULL,outfname,errorfname,abuf);
  262.   if(open_working_file(&wf,varfname)==0) {
  263.     char *pt, *pv;
  264.     while(wgetline(tmplbuf,MAX_LINELEN, &wf)!=EOF) {
  265.       pt=find_word_start(tmplbuf); pv=strchr(pt,'=');
  266.       if(pv==NULL || pv<=tmplbuf) continue;
  267.       *pv=0; pv=find_word_start(++pv);
  268.       *find_word_end(pt)=0;
  269.       strip_trailing_spaces(pv);
  270.       setvar(pt,pv);
  271.     }
  272.     close_working_file(&wf,0);
  273.   }
  274.   read_tmp_file(p,"exec.out");
  275.   read_tmp_file(obuf,"exec.type");
  276.   if(obuf[0]) k=atoi(obuf); else k=0;
  277.   for(i=2;i<=k && i<256;i++) {
  278.     char nbuf[32];
  279.     snprintf(nbuf,sizeof(nbuf),"exec.out.%d",i);
  280.     read_tmp_file(obuf,nbuf);
  281.     if(obuf[0]) {
  282.       snprintf(nbuf,sizeof(nbuf),"wims_exec_out_%d",i);
  283.       force_setvar(nbuf,obuf);
  284.     }
  285.   }
  286.   strip_trailing_spaces(p);
  287.   _get_exec_error(errorfname,namebuf);
  288.   return;
  289. }
  290.  
  291. /* execute external program in the module directory.
  292.  * For privileged modules only
  293.  */
  294. void calc_mexec(char *p)
  295. {
  296.   char *public_bin;
  297.   if(robot_access) return;
  298.   if(trusted_module()!=1 || is_class_module) {
  299.     module_error("not_trusted"); *p=0; return;
  300.   }
  301. /* The following is useless, because mexec content is often given
  302.  * by variables
  303.  */
  304. /*    if(strstr(p,PARENT_DIR_STRING)!=NULL) {
  305.      setvar("wims_error_data",p);
  306.      module_error("illegal_fname"); *p=0; return;
  307.     }
  308. */    public_bin=bin_dir; bin_dir=module_prefix;
  309.   exec_is_module=1;
  310.   calc_exec(p); bin_dir=public_bin;
  311.   exec_is_module=0;
  312. }
  313.  
  314. void _calc_exec(char *p, char *arg0, char *arg1, int n)
  315. {
  316.   char *abuf[8];
  317.   char outfname[MAX_FNAME+1], errorfname[MAX_FNAME+1];
  318.  
  319.   if(robot_access) {*p=0; return;}
  320.   if(!trusted_module() || is_class_module) {
  321.     if(strcasecmp(tmp_debug,"yes")==0)
  322.       accessfile(p,"w","%s/%s.cmd",tmp_dir,arg0);
  323.     abuf[0]="bin/ch..root";
  324.     abuf[1]="&"; abuf[2]=arg0; abuf[3]=p;
  325.     abuf[4]=NULL;
  326.   }
  327.   else {
  328. /* if(strstr(p,PARENT_DIR_STRING)!=NULL) {
  329.          setvar("wims_error_data",p);
  330.          module_error("illegal_fname"); *p=0; return;
  331.      }
  332. */
  333.       abuf[0]=arg0; abuf[1]=arg1; abuf[n]=p; abuf[n+1]=NULL;
  334.   }
  335.   mkfname(outfname,"%s/exec.out",tmp_dir);
  336.   mkfname(errorfname,"%s/exec.err",tmp_dir);
  337.   wrapexec=1; exportall();
  338.   execredirected(abuf[0],NULL,outfname,errorfname,abuf);
  339.   read_tmp_file(p,"exec.out");
  340.   _get_exec_error(errorfname,arg0);
  341. }
  342.  
  343. /* call shell. */
  344. void calc_sh(char *p)
  345. {
  346.   _calc_exec(p,"sh","-c",2);
  347. }
  348.  
  349. /* call perl. */
  350. void calc_perl(char *p)
  351. {
  352.   _calc_exec(p,"perl","-e",2);
  353. }
  354.  
  355. /* simple evaluation of string */
  356. void calc_evalue(char *p)
  357. {
  358.   double d;
  359.   d=evalue(p); float2str(d,p); return;
  360. }
  361.  
  362. /* substitute math variables */
  363. void calc_mathsubst(char *p)
  364. {
  365.   char *expr, *val, *nam, *pp;
  366.   char buf[MAX_LINELEN+1];
  367.   expr=wordchr(p,"in"); if(expr==NULL) goto error;
  368.   ovlstrcpy(buf,find_word_start(expr+strlen("in")));substit(buf);
  369.   *expr=0; substit(p);
  370.   val=strchr(p,'=');
  371.   if(val==NULL) {
  372.     error:
  373.     module_error("mathsubst_syntax"); *p=0;return;
  374.   }
  375.   nam=find_word_start(p); *val=0;
  376.   val=find_word_start(val+1);
  377.   for(pp=val+strlen(val)-1;pp>=val && isspace(*pp);*pp--=0);
  378.   *find_word_end(nam)=0;
  379.   if(*nam==0) goto error;
  380.   if(*nam) for(pp=varchr(buf,nam);pp!=NULL;pp=varchr(pp,nam)) {
  381.     string_modify(buf,pp,pp+strlen(nam),"%s",val);
  382.     pp+=strlen(val);
  383.   }
  384.   ovlstrcpy(p,buf);
  385. }
  386.  
  387. /* substitute and evaluate. */
  388. void calc_evalsubst(char *p)
  389. {
  390.   calc_mathsubst(p);
  391.   calc_evalue(p);
  392. }
  393.  
  394. /* Nothing needs to be done in the function;
  395.  * substitution is done or not by tag in the structure.
  396.  */
  397. void calc_subst(char *p)
  398. {
  399. }
  400.  
  401. int _randrep(char *p)
  402. {
  403.   char *rep;
  404.   int i;
  405.   rep=wordchr(p,"repeat"); if(rep==NULL) return 1;
  406.   *rep=0; rep+=strlen("repeat"); i=evalue(rep);
  407.   if(i<1) i=1;
  408.   if(i>MAX_VALUE_LIST) i=MAX_VALUE_LIST;
  409.   return i;
  410. }
  411.  
  412. /* integer random */
  413. void calc_randint(char *p)
  414. {
  415.   int lbound, ubound, n, i, t;
  416.   char *p1, *parm[2];
  417.  
  418.   t=_randrep(p); n=cutitems(p,parm,2);
  419.   if(n<=0) {
  420.     snprintf(p,3,"0"); return; /* Random without bound: return 0. */
  421.   }
  422.   lbound=evalue(parm[0]);
  423. /* Missing ubound: random between +-1 and lbound. */
  424.   if(n<=1) {if(lbound<0) ubound=-1; else ubound=1;}
  425.   else ubound=evalue(parm[1]);
  426.   if(lbound>ubound) {i=lbound; lbound=ubound; ubound=i;}
  427.   for(i=0,p1=p;i<t;i++) {
  428.     if(i>0) *p1++=',';
  429.     mystrncpy(p1,int2str((int) irand(ubound-lbound+1)+lbound),
  430.             MAX_LINELEN-(p1-p)-1);
  431.     p1+=strlen(p1);
  432.   }
  433.   return;
  434. }
  435.  
  436. /* floating random */
  437. void calc_randdouble(char *p)
  438. {
  439.   double lbound, ubound;
  440.   int n, i, t;
  441.   char *parm[2], *p1;
  442.  
  443.   t=_randrep(p); n=cutitems(p,parm,2);
  444.   if(n<=0) {
  445.     snprintf(p,3,"0"); return; /* Random without bound: return 0. */
  446.   }
  447.   lbound=evalue(parm[0]);
  448.   if(n<=1) ubound=0; /* Missing ubound: random between 0 and lbound. */
  449.   else ubound=evalue(parm[1]);
  450.   for(i=0,p1=p;i<t;i++) {
  451.     float2str(drand(ubound-lbound)+lbound,tmplbuf);
  452.     mystrncpy(p1,tmplbuf,MAX_LINELEN-(p1-p));
  453.     p1+=strlen(p1);
  454.     if(i<t-1 && p1-p<MAX_LINELEN) *p1++=',';
  455.   }
  456.   *p1=0; return;
  457. }
  458.  
  459. /* Takes randomly a record in a datafile */
  460. void calc_randfile(char *p)
  461. {
  462.   char *pp, n[MAX_FNAME+1];
  463.   int i, j;
  464.  
  465.   pp=find_word_start(p); *find_word_end(pp)=0;
  466.   mystrncpy(n,pp,sizeof(n));
  467.   i=datafile_recordnum(pp);
  468.   if(i<=0) {  /* zero records */
  469.     *p=0; return;
  470.   }
  471.   j=irand(i); datafile_fnd_record(n,j+1,p);
  472.   return;
  473. }
  474.  
  475. /* random char, word, line, item in a string */
  476. void calc_randchar(char *p)
  477. {
  478.   p[0]=p[(int) irand(strlen(p))];p[1]=0;
  479. }
  480.  
  481. void calc_randitem(char *p)
  482. {
  483.   int i;
  484.   i=itemnum(p);
  485.   (void) fnd_item(p,irand(i)+1,tmplbuf);
  486.   mystrncpy(p,tmplbuf,MAX_LINELEN);
  487. }
  488.  
  489. void calc_randline(char *p)
  490. {
  491.   int i;
  492.   i=linenum(p); fnd_line(p,irand(i)+1,tmplbuf);
  493.   mystrncpy(p,tmplbuf,MAX_LINELEN);
  494. }
  495.  
  496. void calc_randrow(char *p)
  497. {
  498.   rows2lines(p); calc_randline(p);
  499. }
  500.  
  501. void calc_randword(char *p)
  502. {
  503.   int i;
  504.   i=wordnum(p); fnd_word(p,irand(i)+1,tmplbuf);
  505.   mystrncpy(p,tmplbuf,MAX_LINELEN);
  506. }
  507.  
  508. /* random permutation of {1,...,n} */
  509. void calc_randperm(char *p)
  510. {
  511.   int n, i, j, k, t, pt, type;
  512.   double v;
  513.   char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1];
  514.   char *list[MAX_RANDPERM];
  515.   int table[MAX_RANDPERM];
  516.   char *p1,*p2,*pp;
  517.  
  518.   t=0; pt=0; pp=p;
  519.   p1=find_word_start(p); p2=find_word_end(p1);
  520.   if(p2-p1==strlen("even") && strncasecmp(p1,"even",strlen("even"))==0) {
  521.     t=1; pp=p2;
  522.   }
  523.   if(p2-p1==strlen("odd") && strncasecmp(p1,"odd",strlen("odd"))==0) {
  524.     t=-1; pp=p2;
  525.   }
  526.  
  527.   p1=find_item_end(pp);
  528.   if(*p1==',') {
  529.     type=1; n=cutitems(pp,list,MAX_RANDPERM); v=0;
  530.   }
  531.   else {
  532.     v=evalue(pp); n=v; type=0;
  533.   }
  534.   if(n==1 && !isfinite(v)) {ovlstrcpy(p,pp); goto shorder;}
  535.   if(n<=0) {*p=0; return;}
  536.   if(n==1 && type==0) {
  537.     ovlstrcpy(p,"1");
  538.     shorder: force_setvar("wims_shuffle_order","1"); return;
  539.   }
  540.   if(n>MAX_RANDPERM) n=MAX_RANDPERM;
  541.   for(i=0;i<n;i++) table[i]=i;
  542. /* Uniformity of this algorithm is easy to show by induction. */
  543.   for(i=0;i<n-1;i++) {
  544.     j=irand(n-i)+i; if(j==i) continue;
  545.     k=table[i]; table[i]=table[j]; table[j]=k;
  546.     pt++;
  547.   }
  548.   pt&=1;
  549.   if((t==1 && pt==1) || (t==-1 && pt==0)) {
  550.     k=table[0]; table[0]=table[1]; table[1]=k;
  551.   }
  552.   if(type) {mystrncpy(buf1,list[table[0]],MAX_LINELEN); p1=buf1+strlen(buf1);}
  553.   else buf1[0]=0;
  554.   mystrncpy(buf2,int2str(table[0]+1),MAX_LINELEN); p2=buf2+strlen(buf2);
  555.   for(i=1;i<n;i++) {
  556.     if(type) {
  557.       j=strlen(list[table[i]]);
  558.       if(p1-buf1+j>=MAX_LINELEN-1) module_error("cmd_output_too_long");
  559.       *p1++=','; memmove(p1,list[table[i]],j+1); p1+=j;
  560.     }
  561.     *p2++=',';
  562.     mystrncpy(p2,int2str(table[i]+1), MAX_LINELEN-(p2-buf2)-1);
  563.     p2+=strlen(p2);
  564.   }
  565.   if(type) mystrncpy(p,buf1,MAX_LINELEN);
  566.   else mystrncpy(p,buf2,MAX_LINELEN);
  567.   force_setvar("wims_shuffle_order",buf2);
  568. }
  569.  
  570. /* Computes number of lines in the parm. */
  571. void calc_linenum(char *p)
  572. {
  573.   int i=linenum(p); mystrncpy(p,int2str(i),MAX_LINELEN);
  574. }
  575.  
  576. /* Computes number of lines in the parm. */
  577. void calc_rownum(char *p)
  578. {
  579.   int i=rownum(p); mystrncpy(p,int2str(i),MAX_LINELEN);
  580. }
  581.  
  582. /* Computes number of items in the list p. */
  583. void calc_itemnum(char *p)
  584. {
  585.   int i=itemnum(p); mystrncpy(p,int2str(i),MAX_LINELEN);
  586. }
  587.  
  588. /* Computes number of records in the datafile p. */
  589. void calc_recordnum(char *p)
  590. {
  591.   int i;
  592.   i=datafile_recordnum(p); mystrncpy(p,int2str(i),MAX_LINELEN);
  593. }
  594.  
  595. /* Computes number of words in the parm. */
  596. void calc_wordnum(char *p)
  597. {
  598.   int i=wordnum(p); mystrncpy(p,int2str(i),MAX_LINELEN);
  599. }
  600.  
  601. /* string length */
  602. void calc_lengthof(char *p)
  603. {
  604.   int i;
  605. /* Strip leading and trailing spaces? */
  606.   i=strlen(p); mystrncpy(p,int2str(i),MAX_LINELEN);
  607. }
  608.  
  609. char *_blockof_one(char *buf,
  610.             unsigned int len,
  611.             char *(fnd_fn)(char *pl, int n, char bbuf[]),
  612.             int i, char bbuf[])
  613. {
  614.   if(i<0 || i>len || (i==0 && fnd_fn!=datafile_fnd_record) ) {
  615.     bbuf[0]=0; return bbuf;
  616.   }
  617.   return fnd_fn(buf,i,bbuf);
  618. }
  619.  
  620. void _blockof(char *p,
  621.            unsigned int (len_fn)(char *pl),
  622.            char *(fnd_fn)(char *pl, int n, char bbuf[]),
  623.            char *append_char,
  624.            char *msg_str)
  625. {
  626.   int i,j,l,started;
  627.   char *pp, *pe, *po, *pnext;
  628.   char buf[MAX_LINELEN+1], obuf[MAX_LINELEN+1], bbuf[MAX_LINELEN+1];
  629.   pp=wordchr(p,"of");
  630.   if(pp==NULL) {
  631.     setvar(error_data_string,msg_str);
  632.     module_error("no_of"); *p=0; return;
  633.   }
  634.   *pp=0; pp=find_word_start(pp+strlen("of")+1);
  635.   mystrncpy(buf,pp,sizeof(buf)); substit(buf);
  636.   obuf[0]=0; started=0; po=obuf;
  637.   pp=strparstr(p,"to");
  638.   while(*pp!=0 && ((pp>p && !isspace(*(pp-1))) || !isspace(*(pp+2))))
  639.     pp=strparstr(pp+2,"to");
  640.   if(*pp==0) pp=strparstr(p,"..");
  641.   if(*pp!=0) {
  642.     int t=len_fn(buf);
  643.     *pp=0; pe=find_word_start(pp+2);
  644.     i=evalue(p); j=evalue(pe); pnext=buf;
  645.     if(i<0) i=t+i+1;
  646.     if(i<1) i=1;
  647.     if(j<0) j=t+j+1;
  648.     if(j>t) j=t;
  649.     for(; i<=j; i++) {
  650.       if(started==0 || fnd_fn==datafile_fnd_record)
  651.         pp=_blockof_one(buf,t,fnd_fn,i,bbuf);
  652.       else
  653.         pp=_blockof_one(pnext,1,fnd_fn,1,bbuf);
  654.       l=strlen(pp); pnext=fnd_nextpos;
  655.       if(l+(po-obuf)>=MAX_LINELEN-3) {
  656.         too_long: user_error("cmd_output_too_long"); return;
  657.       }
  658.       if(started) {ovlstrcpy(po,append_char); po+=strlen(po);}
  659.       memmove(po,pp,l); po+=l; *po=0; started++;
  660.     }
  661.   }
  662.   else {
  663.     char *p1,*p2,ibuf[MAX_LINELEN+1];
  664.     int t=len_fn(buf);
  665.     mystrncpy(ibuf,p,sizeof(ibuf)); substit(ibuf);
  666.     for(p1=ibuf;*p1;p1=find_word_start(p2)) {
  667.       p2=find_item_end(p1); if(*p2) *p2++=0;
  668.       i=evalue(p1);
  669.       if(i<0) i=t+i+1;
  670.       if(i>t || i<0) continue;
  671.       pp=_blockof_one(buf,t,fnd_fn,i,bbuf);l=strlen(pp);
  672.       if(l+(po-obuf)>=MAX_LINELEN-3) goto too_long;
  673.       if(started) {ovlstrcpy(po,append_char); po+=strlen(po);}
  674.       memmove(po,pp,l); po+=l; *po=0; started++;
  675.     }
  676.   }
  677.   mystrncpy(p,obuf,MAX_LINELEN);
  678. }
  679.  
  680. /* pick lines */
  681. void calc_lineof(char *p)
  682. {
  683.   _blockof(p,linenum,fnd_line,"\n","line");
  684. }
  685.  
  686. /* pick rows */
  687. void calc_rowof(char *p)
  688. {
  689.   char *cc, tbuf[MAX_LINELEN+1]; /* called by substit(); no right to use tmplbuf */
  690.   mystrncpy(tbuf,p,sizeof(tbuf)); substit(tbuf);
  691.   if(strchr(tbuf,'\n')==NULL && strchr(tbuf,';')!=NULL) cc=";";
  692.   else cc="\n";
  693.   _blockof(p,rownum,fnd_row,cc,"row");
  694. }
  695.  
  696. /* pick items */
  697. void calc_itemof(char *p)
  698. {
  699.   _blockof(p,itemnum,fnd_item,", ","item");
  700. }
  701.  
  702. /* pick records in datafile */
  703. void calc_recordof(char *p)
  704. {
  705.   _blockof(p,datafile_recordnum,datafile_fnd_record,"\n:","record");
  706. }
  707.  
  708. /* pick words */
  709. void calc_wordof(char *p)
  710. {
  711.   _blockof(p,wordnum,fnd_word," ","word");
  712. }
  713.  
  714. /* pick characters */
  715. void calc_charof(char *p)
  716. {
  717.   _blockof(p,charnum,fnd_char,"","char");
  718. }
  719.  
  720. char *append_obj[]={
  721.   "item","line","word","semicolon","colon"
  722. };
  723. char apch_list[]={',','\n',' ',';',':'};
  724. #define append_obj_no (sizeof(append_obj)/sizeof(append_obj[0]))
  725.  
  726. /* append object to string */
  727. void calc_append(char *p)
  728. {
  729.   char append_char, *p1,*p2,*p3,*p4;
  730.   int i,l1,l2;
  731.   p1=find_word_start(p);p2=find_word_end(p1);
  732.   if(*p2!=0) *p2++=0;
  733.   for(i=0;i<append_obj_no && strcmp(p1,append_obj[i])!=0;i++);
  734.   if(i>=append_obj_no) {
  735.     synterr:
  736.     module_error("append_syntax");
  737.     *p=0; return;
  738.   }
  739.   append_char=apch_list[i];
  740.   p3=wordchr(p2,"to");
  741.   if(p3==NULL) goto synterr;
  742.   for(p4=p3-1;p4>p2 && isspace(*(p4-1));p4--);
  743.   if(p4<=p2) goto synterr;
  744.   *p4=0;p3=find_word_start(p3+strlen("to"));
  745.   memmove(tmplbuf,p2,p4-p2); tmplbuf[p4-p2]=0; ovlstrcpy(p,p3);
  746.   substit(tmplbuf);substit(p);
  747.   l1=strlen(p); l2=strlen(tmplbuf);
  748.   if(l1+l2>=MAX_LINELEN-1) user_error("cmd_output_too_long");
  749.   p3=find_word_start(p); p4=p+l1;
  750.   if(*p3) *p4++=append_char;
  751.   memmove(p4,tmplbuf,l2); p4[l2]=0;
  752. }
  753.  
  754. /* character translation */
  755. void calc_translate(char *p)
  756. {
  757.   int i, internal;
  758.   char *q[3];
  759.   char bf[3][MAX_LINELEN+1];
  760.   char fn[3][MAX_FNAME+1], *arglist[8];
  761.  
  762.   q[0]=find_word_start(p); internal=0;
  763.   if(strncasecmp(q[0],"internal",strlen("internal"))==0 &&
  764.      isspace(*(q[0]+strlen("internal")))) {
  765.     q[0]=find_word_start(q[0]+strlen("internal"));
  766.     internal=1;
  767.   }
  768.   q[1]=wordchr(q[0],"to"); q[2]=wordchr(q[0],"in");
  769.   if(q[1]==NULL || q[2]==NULL) {
  770.     module_error("tr_syntax"); *p=0; return;
  771.   }
  772.   *q[1]=0; *q[2]=0;
  773.   q[1]=find_word_start(q[1]+strlen("to"));
  774.   q[2]=find_word_start(q[2]+strlen("in"));
  775.   for(i=0;i<3;i++) {
  776.     strip_trailing_spaces(q[i]);
  777.     mystrncpy(bf[i],q[i],sizeof(bf[i]));substit(bf[i]);
  778.   }
  779.   if(bf[0][0]==0) goto bailout;
  780.   if(internal || strstr(tmp_dir,"sessions")==NULL || (strpbrk(bf[0],"\\[-")==NULL &&
  781.      strpbrk(bf[1],"\\[-")==NULL)) { /* direct internal translation */
  782.     char *pp;
  783.     if(strlen(bf[1])<strlen(bf[0])) bf[0][strlen(bf[1])]=0;
  784.     for(pp=strpbrk(bf[2],bf[0]);pp!=NULL && *pp!=0 && pp<bf[2]+MAX_LINELEN;
  785.        pp=strpbrk(pp+1,bf[0])) {
  786.       for(i=0;bf[0][i]!=*pp && bf[0][i]!=0;i++);
  787.       if(bf[0][i]!=0) *pp=bf[1][i];
  788.     }
  789.     bailout: mystrncpy(p,bf[2],MAX_LINELEN);
  790.     return;
  791.   }
  792.   mkfname(fn[0],"%s/tr.in",tmp_dir);
  793.   mkfname(fn[1],"%s/tr.out",tmp_dir);
  794.   accessfile(bf[2],"w",fn[0]);
  795.   arglist[0]=tr_prog; arglist[1]=bf[0]; arglist[2]=bf[1];
  796.   arglist[3]=NULL; exportall();
  797.   execredirected(tr_prog,fn[0],fn[1],"/dev/null",arglist);
  798.   read_tmp_file(p,"tr.out");
  799.   strip_trailing_spaces(p);
  800. }
  801.  
  802. /* internal common routine for positionof */
  803. void _pos(char *hay, char *stitch, int style, char *out,
  804.        char *(fnd_obj)(char *p, int n, char bf[]),
  805.        unsigned int (objnum)(char *p))
  806. {
  807.   int i,n,t;
  808.   char buf[MAX_LINELEN+1], nbuf[10];
  809.  
  810.   n=objnum(hay);
  811.   for(i=1;i<=n;i++) {
  812.     fnd_obj(hay,i,buf);
  813.     if(strcmp(buf,stitch)!=0) continue;
  814.     t=strlen(out); if(t>MAX_LINELEN-12) return;
  815.     if(t>0) strcat(out,",");
  816.     snprintf(nbuf,sizeof(nbuf),"%d",i); strcat(out,nbuf);
  817.   }
  818. }
  819.  
  820. /* return positions of searched for objects */
  821. void calc_pos(char *p)
  822. {
  823.   char buf[2][MAX_LINELEN+1];
  824.   char *p1, *p2;
  825.   int  style;
  826.  
  827.   p1=find_word_start(p); p2=wordchr(p1,"in");
  828.   if(p2==NULL) module_error("syntax_error");
  829.   *p2=0;p2=find_word_start(p2+strlen("in"));
  830.   strip_trailing_spaces(p1);
  831.   ovlstrcpy(buf[0],p1);*find_word_end(buf[0])=0; style=0;
  832.   if(strcmp(buf[0],"word")==0) style=1;
  833.   else {
  834.     if(strcmp(buf[0],"item")==0) style=2;
  835.     else {
  836.       if(strcmp(buf[0],"line")==0) style=3;
  837.       else {
  838.         if(strcmp(buf[0],"char")==0) style=4;
  839.       }
  840.     }
  841.   }
  842.   if(style>0) p1=find_word_start(find_word_end(p1));
  843.   ovlstrcpy(buf[0],p1); ovlstrcpy(buf[1],p2);
  844.   substit(buf[0]); substit(buf[1]); *p=0;
  845.   switch(style) {
  846.     case 0: {  /* string */
  847.       char *pp, nbuf[10];
  848.       int i,t;
  849.       for(pp=strstr(buf[1],buf[0]);pp!=NULL;pp=strstr(pp+1,buf[0])) {
  850.         i=pp-buf[1]; t=strlen(p); if(t>MAX_LINELEN-12) return;
  851.         if(t>0) strcat(p,",");
  852.         snprintf(nbuf,sizeof(nbuf),"%d",i); strcat(p,nbuf);
  853.       }
  854.       return;
  855.     }
  856.     case 1: { /* word */
  857.       _pos(buf[1],buf[0],style,p,fnd_word,wordnum);
  858.       return;
  859.     }
  860.     case 2: { /* item */
  861.       _pos(buf[1],buf[0],style,p,fnd_item,itemnum);
  862.       return;
  863.     }
  864.     case 3: { /* line */
  865.       _pos(buf[1],buf[0],style,p,fnd_line,linenum);
  866.       return;
  867.     }
  868.     case 4: { /* char */
  869.       _pos(buf[1],buf[0],style,p,fnd_char,charnum);
  870.       return;
  871.     }
  872.   }
  873. }
  874.  
  875. /* internal routine for calc_replace. */
  876. void _obj_replace(char *orig, char *by, char *in, int num,
  877.             char separator, char *result,
  878.             char *(fnd_obj)(char *p, int n, char bf[]),
  879.             unsigned int (objnum)(char *p),
  880.             char *(objchr)(char *p,char *w))
  881. {
  882.   int i;
  883.   char *p1, *p2;
  884.  
  885.   ovlstrcpy(result,in);
  886.   if(num!=0) {
  887.     num=objnum(in); i=evalue(orig);
  888.     if(i==0) module_error("bad_index");
  889.     if(i<0) i=num+i+1;
  890.     if(i>num || i<1) return;
  891.     if(separator==0) {  /* char */
  892.       result[i-1]=by[0]; return;
  893.     }
  894.     fnd_obj(result,i,orig); p1=fnd_position;
  895.     if(i<num) {
  896.       fnd_obj(result,i+1,orig); p2=fnd_position;
  897.     }
  898.     else p2=result+strlen(result);
  899.     if(p1==NULL || p2==NULL) internal_error("_obj_replace() error.");
  900.     if(i<num) {
  901.       i=strlen(by); by[i++]=separator;by[i]=0;
  902.     }
  903.     string_modify(result,p1,p2,"%s",by);
  904.   }
  905.   else {
  906.     if(separator==0) {
  907.       if(orig[0]==0 || by[0]==0) return;
  908.       for(p1=strchr(result,orig[0]);p1!=NULL;p1=strchr(p1+1,orig[0]))
  909.         *p1=by[0];
  910.       return;
  911.     }
  912.     if(strlen(orig)+strlen(by)==0) return;
  913.     for(p1=objchr(result,orig);p1!=NULL;p1=objchr(p1+strlen(by)+1,orig)) {
  914.          string_modify(result,p1,p1+strlen(orig),"%s",by);
  915.     }
  916.   }
  917. }
  918.  
  919. /* replacement */
  920. void calc_replace(char *p)
  921. {
  922.   int i,style,num,internal;
  923.   char *q[3], *pp;
  924.   char bf[4][MAX_LINELEN+17];
  925.   char fn[3][MAX_FNAME+1], *arglist[8];
  926.   char regexp_char='/';
  927.  
  928.   style=num=0;
  929.   q[0]=find_word_start(p); internal=0;
  930.   if(strncasecmp(q[0],"internal",strlen("internal"))==0 &&
  931.      isspace(*(q[0]+strlen("internal")))) {
  932.     q[0]=find_word_start(q[0]+strlen("internal"));
  933.     internal=1;
  934.   }
  935.   q[1]=wordchr(q[0],"by"); q[2]=wordchr(q[0],"in");
  936.   if(q[1]==NULL || q[2]==NULL) {
  937.     module_error("replace_syntax"); *p=0; return;
  938.   }
  939.   *q[1]=0; *q[2]=0;
  940.   q[1]=find_word_start(q[1]+strlen("by"));
  941.   q[2]=find_word_start(q[2]+strlen("in"));
  942.   mystrncpy(bf[0],q[0],sizeof(bf[0]));
  943.   pp=find_word_end(bf[0]); if(*pp) *(pp++)=0;
  944.   if(strcmp(bf[0],"word")==0) style=1;
  945.   else {
  946.     if(strcmp(bf[0],"item")==0) style=2;
  947.     else {
  948.       if(strcmp(bf[0],"line")==0) style=3;
  949.       else {
  950.         if(strcmp(bf[0],"char")==0) style=4;
  951.       }
  952.     }
  953.   }
  954.   if(style>0) {
  955.     q[0]=find_word_start(find_word_end(q[0]));
  956.     mystrncpy(bf[0],q[0],sizeof(bf[0]));
  957.     pp=find_word_end(bf[0]); if(*pp) *(pp++)=0;
  958.     if(strcmp(bf[0],"number")==0) {
  959.       num=1; q[0]=find_word_start(pp);
  960.     }
  961.   }
  962.   for(i=0;i<3;i++) {
  963.     strip_trailing_spaces(q[i]);
  964.     mystrncpy(bf[i],q[i],sizeof(bf[i]));
  965.     substit(bf[i]);
  966.   }
  967.   if(bf[0][0]==0) {mystrncpy(p,bf[2],MAX_LINELEN); return;}
  968.   switch(style) {
  969.     case 1: { /* word */
  970.       _obj_replace(bf[0],bf[1],bf[2],num,' ',p,
  971.               fnd_word,wordnum,wordchr);
  972.       return;
  973.     }
  974.     case 2: { /* item */
  975.       _obj_replace(bf[0],bf[1],bf[2],num,',',p,
  976.               fnd_item,itemnum,itemchr);
  977.       return;
  978.     }
  979.     case 3: { /* line */
  980.       _obj_replace(bf[0],bf[1],bf[2],num,'\n',p,
  981.               fnd_line,linenum,linechr);
  982.      return;
  983.     }
  984.     case 4: { /* char */
  985.       if(bf[1][0]==0) bf[1][0]=' ';
  986.       if(bf[0][0]==0) return;
  987.       _obj_replace(bf[0],bf[1],bf[2],num,0,p,
  988.               fnd_char,charnum,charchr);
  989.       return;
  990.     }
  991.     default: break;
  992.   }
  993.   if(internal || strstr(tmp_dir,"sessions")==NULL || (strpbrk(bf[0],"\\[^.*$")==NULL &&
  994.        strpbrk(bf[1],"\\[^.*$")==NULL)) {
  995. /* No regexp, direct replace */
  996.     char *pp;
  997.     for(pp=strstr(bf[2],bf[0]);pp<bf[2]+MAX_LINELEN && pp!=NULL;
  998.        pp=strstr(pp+strlen(bf[1]),bf[0])) {
  999.       string_modify(bf[2],pp,pp+strlen(bf[0]),"%s",bf[1]);
  1000.     }
  1001.     mystrncpy(p,bf[2],MAX_LINELEN);return;
  1002.   }
  1003.   mkfname(fn[0],"%s/sed.in",tmp_dir);
  1004.   mkfname(fn[1],"%s/sed.out",tmp_dir);
  1005.   accessfile(bf[2],"w",fn[0]);
  1006.   snprintf(bf[3],sizeof(bf[3]),"s%c%s%c%s%cg",
  1007.           regexp_char,bf[0],regexp_char,bf[1],regexp_char);
  1008.   arglist[0]=sed_prog; arglist[1]=bf[3]; arglist[2]=NULL;
  1009.   execredirected(sed_prog,fn[0],fn[1],"/dev/null",arglist);
  1010.   read_tmp_file(p,"sed.out");
  1011.   strip_trailing_spaces(p);
  1012. }
  1013.  
  1014. /* transforms a string to hexadecimal code, upper-case */
  1015. void calc_hex(char *p)
  1016. {
  1017.   unsigned char *p1, orig[MAX_LINELEN+1];
  1018.   char *p2;
  1019.   char *hex="0123456789ABCDEF";
  1020.  
  1021.   mystrncpy((char*)orig,p,MAX_LINELEN);
  1022.   for(p1=orig, p2=p; *p1 && p2-p<MAX_LINELEN-2; p1++) {
  1023.     *p2++=hex[(*p1>>4)&15]; *p2++=hex[*p1&15];
  1024.   }
  1025.   *p2=0;
  1026. }
  1027.  
  1028. /* transforms to lower/upper cases */
  1029. void calc_tolower(char *p)
  1030. {
  1031.   char *pp;
  1032.   for(pp=p;*pp;pp++) *pp=tolower(*pp);
  1033. }
  1034.  
  1035. void calc_toupper(char *p)
  1036. {
  1037.   char *pp;
  1038.   for(pp=p;*pp;pp++) *pp=toupper(*pp);
  1039. }
  1040.  
  1041. /* strip leading and trailing spaces */
  1042. void calc_trim(char *p)
  1043. {
  1044.   char *s;
  1045.   s=find_word_start(p);
  1046.   if(s>p) memmove(p,s,MAX_LINELEN-(s-p)+1);
  1047.   strip_trailing_spaces(p);
  1048. }
  1049.  
  1050. /* output date. Uses Linux 'date' utility. */
  1051. void calc_date(char *p)
  1052. {
  1053.   char *p1, *p2, *p3;
  1054.   if(!trusted_module() || is_class_module) {
  1055.     if(strstr(p,"..")!=NULL) return;
  1056.     for(p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
  1057.       p2=find_word_end(p1);
  1058.       while(*p1=='\'' || *p1=='"') p1++;
  1059.       if(*p1!='-') continue;
  1060.       for(p3=p1+1;p3<p2;p3++) if(strchr("rs",*p3)!=NULL) return;
  1061.     }
  1062.   }
  1063.   wrapexec=1;
  1064.   call_ssh("date %s >%s/date.out 2>/dev/null",p,tmp_dir);
  1065.   read_tmp_file(p,"date.out");
  1066. }
  1067.  
  1068. /* ls, or dir */
  1069. void calc_listfile(char *p)
  1070. {
  1071.   char *pp;
  1072.  
  1073. /* only for trusted modules */
  1074.   if(!trusted_module() || is_class_module) {
  1075.     module_error("not_trusted"); *p=0; return;
  1076.   }
  1077. /* security measures. */
  1078.   for(pp=p;*pp;pp++) if(isspace(*pp) || *pp==';') *pp=' ';
  1079.   if(strstr(p,parent_dir_string)!=NULL) {
  1080.     setvar(error_data_string,p);
  1081.     module_error("illegal_fname"); return;
  1082.   }
  1083.   wrapexec=1;
  1084.   call_sh("ls %s >%s/ls.out 2>%s/ls.err",
  1085.          p,tmp_dir,tmp_dir);
  1086.   read_tmp_file(p,"ls.out");
  1087. }
  1088.  
  1089. void calc_fileexists(char *p)
  1090. {
  1091.   char *pp, *fname;
  1092. /* security measures. */
  1093.   for(pp=p;*pp;pp++) if(isspace(*pp) || *pp==';') *pp=' ';
  1094.   if(strstr(p,parent_dir_string)!=NULL) {
  1095.     setvar(error_data_string,p);
  1096.     module_error("illegal_fname"); return mystrncpy(p,"no",MAX_LINELEN);
  1097.   }
  1098.   if(strncmp(p,"wimshome/",9)==0){
  1099.     if(robot_access || !trusted_module() || is_class_module){
  1100.       module_error("illegal_fname"); return mystrncpy(p,"no",MAX_LINELEN);
  1101.     }
  1102.     else
  1103.     {
  1104.       fname=strchr(p,'/');
  1105.       if(ftest(mkfname(NULL,"%s/%s",getvar("wims_home"),fname))==is_file)
  1106.         return mystrncpy(p,"yes",MAX_LINELEN);
  1107.     }
  1108.   }
  1109.   if(ftest(mkfname(NULL,"%s",p))==is_file) return mystrncpy(p,"yes",MAX_LINELEN);
  1110.   else return mystrncpy(p,"no",MAX_LINELEN);
  1111. }
  1112.  
  1113. /* instex static: static tex inserts */
  1114. void calc_instexst(char *p)
  1115. {
  1116.   char nbuf[MAX_FNAME+1], bufc[MAX_LINELEN+1];
  1117.   char buf2[1024], altbuf[1024], buf[MAX_LINELEN+1], urlbuf[MAX_LINELEN+1];
  1118.   char *b, *at, *al, *md1, *md2;
  1119.   int t, border, vspace;
  1120.   struct stat st,stc;
  1121.   char *p1, *p2, *p3, *ppp, *pt;
  1122.  
  1123.   if(robot_access) {*p=0; return;}
  1124.   p1=find_word_start(p); p2=find_word_end(p1);
  1125.   p3=p2-4; vspace=0;
  1126.   fix_tex_size();
  1127.   t=untrust; untrust=0;
  1128.   if(find_module_file(m_file.name,bufc,0)) module_error(m_file.name);
  1129.   else stat(bufc,&stc);
  1130.   untrust=t;
  1131.   if(*p3=='.' && (memcmp(p3+1,"gif",3)==0 || memcmp(p3+1,"png",3)==0)) {
  1132.     char mbuf[MAX_LINELEN+1];
  1133.     if(*p2!=0) *p2++=0;
  1134.     p2=find_word_start(p2);
  1135.     ovlstrcpy(mbuf,p1); substit(mbuf);
  1136.     if(strstr(mbuf,parent_dir_string)!=NULL) {
  1137.       setvar(error_data_string,mbuf);
  1138.       module_error("illegal_fname"); return;
  1139.     }
  1140.     mkfname(nbuf,"%s/%s",module_prefix,mbuf);
  1141.   }
  1142.   else {
  1143.     ppp=getvar(ro_name[ro_module]);
  1144.     if(ppp==NULL) internal_error("calc_instexst(): module name vanishes.");
  1145.     p2=p1;
  1146.     mkfname(nbuf,"w/instex/%d/%s/%s_%d.gif",
  1147.            current_tex_size,ppp,m_file.name,m_file.l);
  1148.   }
  1149.   snprintf(urlbuf,sizeof(urlbuf),"%s%s?%X",ref_base,nbuf,
  1150.           (unsigned short int) stc.st_mtime);
  1151.   mystrncpy(buf,nbuf,sizeof(buf));
  1152.   if((ppp=strrchr(buf,'/'))!=NULL) {
  1153.     *ppp=0;mkdirs(buf);
  1154.   }
  1155.   b=getvar("ins_border");
  1156.   at=getvar("ins_attr");
  1157.   al=getvar("ins_align");
  1158.   if(at==NULL) at="";
  1159.   if(al==NULL) al="";
  1160.   al=find_word_start(al);
  1161.   if(*al!=0) snprintf(buf2,sizeof(buf2),"vertical-align:%s",al); else buf2[0]=0;
  1162.   if(b==NULL || *b==0) border=0;
  1163.   else border=atoi(b);
  1164.   if(border<0) border=0;
  1165.   if(border>100) border=100;
  1166.   if(instex_ready(p2,urlbuf)) goto prt;
  1167.   if(stat(nbuf,&st)!=0 || st.st_mtime<stc.st_mtime || st.st_size<45) {
  1168.     setenv("texgif_style",instex_style,1);
  1169.     setenv("texgif_src",p2,1);
  1170.     setenv("texgif_outfile",nbuf,1);
  1171.     setenv("texgif_tmpdir",tmp_dir,1); exportall();
  1172.     wrapexec=0; call_ssh("%s &>%s/instexst.log",tex2gif,tmp_dir);
  1173.     setenv("instexst_src","",1);
  1174.   }
  1175.   prt: md1=md2="";
  1176.   if(strcasecmp(al,"middle")==0) {
  1177.    md1=mathalign_sup1; md2=mathalign_sup2;
  1178.    vspace=5;
  1179.   }
  1180.   if(ins_alt[0]==0) mystrncpy(ins_alt,p2,sizeof(ins_alt));
  1181.   if(strchr(ins_alt,'"')!=NULL || strlen(ins_alt)>256) ins_alt[0]=0;
  1182.   pt=getvar("wims_ins_alt"); if(pt==NULL) pt="";
  1183.   if(strcmp(pt,"empty")==0) altbuf[0]=0;
  1184.   else
  1185.     if(ins_alt[0] && strcmp(pt,"none")!=0)
  1186.       snprintf(altbuf,sizeof(altbuf)," alt=\"\"");
  1187.     else
  1188.       snprintf(altbuf,sizeof(altbuf)," alt=\"%s\"",ins_alt);
  1189.   snprintf(p,MAX_LINELEN,"%s<img src=\"%s\" style=\"border:solid;border-width:%dpx;margin-bottom:%dpx;%s\" %s %s />%s",
  1190.         md1,urlbuf,border,vspace,buf2,at,altbuf,md2);
  1191.   setvar("ins_attr",""); ins_alt[0]=0;
  1192.   setvar("ins_url",urlbuf);
  1193. }
  1194.  
  1195. /* extract non-empty lines or items */
  1196. void calc_nonempty(char *p)
  1197. {
  1198.   int type, i, cnt;
  1199.   char *p1, *p2, buf[MAX_LINELEN+1], out[MAX_LINELEN+1];
  1200.   p1=find_word_start(p); p2=find_word_end(p1);
  1201.   if(*p2) *p2++=0; else {
  1202.     *p=0; return;
  1203.   }
  1204.   type=0;
  1205.   if(strcasecmp(p1,"item")==0 || strcasecmp(p1,"items")==0) type=1;
  1206.   if(type==0 && (strcasecmp(p1,"line")==0 || strcasecmp(p1,"lines")==0))
  1207.     type=2;
  1208.   if(type==0 && (strcasecmp(p1,"row")==0 || strcasecmp(p1,"rows")==0))
  1209.     type=3;
  1210.   if(type==0) module_error("syntax_error");
  1211.   out[0]=out[1]=0;
  1212.   switch(type) {
  1213.     case 1: { /* items */
  1214.       cnt=itemnum(p2);
  1215.       for(i=1; i<=cnt; i++) {
  1216.         fnd_item(p2,i,buf);
  1217.         if(*find_word_start(buf)) {
  1218.           strcat(out,",");strcat(out,buf);
  1219.         }
  1220.       }
  1221.       break;
  1222.     }
  1223.     case 2: { /* lines */
  1224.       lines: cnt=linenum(p2);
  1225.       for(i=1; i<=cnt; i++) {
  1226.         fnd_line(p2,i,buf);
  1227.         if(*find_word_start(buf)) {
  1228.           strcat(out,"\n");strcat(out,buf);
  1229.         }
  1230.       }
  1231.       break;
  1232.     }
  1233.     case 3: { /* rows */
  1234.       int t=rows2lines(p2);
  1235.       if(t==0) goto lines;
  1236.       cnt=linenum(p2);
  1237.       for(i=1; i<=cnt; i++) {
  1238.         fnd_line(p2,i,buf);
  1239.         if(*find_word_start(buf)) {
  1240.           strcat(out,";");strcat(out,buf);
  1241.         }
  1242.       }
  1243.       break;
  1244.     }
  1245.     default: break;
  1246.   }
  1247.   ovlstrcpy(p,out+1);
  1248. }
  1249.  
  1250. /* returns a list with unique items */
  1251. void calc_listuniq(char *p)
  1252. {
  1253.   int i,n;
  1254.   char lout[MAX_LINELEN+2], *ll;
  1255.   char *cut[MAX_LIST];
  1256.   lout[0]=lout[1]=0;
  1257.   n=cutitems(p,cut,MAX_LIST); for(i=0;i<n;i++) {
  1258.     ll=cut[i];
  1259.     if(*ll && itemchr(lout,ll)==NULL &&
  1260.         strlen(lout)+strlen(ll)<MAX_LINELEN-2) {
  1261.       strcat(lout,",");strcat(lout,ll);
  1262.     }
  1263.   }
  1264.   ovlstrcpy(p,lout+1);
  1265. }
  1266.  
  1267. /* returns intersection of 2 lists */
  1268. void calc_listintersect(char *p)
  1269. {
  1270.   char l1[MAX_LINELEN+1],l2[MAX_LINELEN+1],lout[MAX_LINELEN+2];
  1271.   char *cut[MAX_LIST];
  1272.   char *pp, *ll;
  1273.   int i,n;
  1274.  
  1275.   pp=wordchr(p,"and");
  1276.   if(pp==NULL) module_error("syntax_error");
  1277.   *pp=0;ovlstrcpy(l1,p); ovlstrcpy(l2,pp+strlen("and"));
  1278.   lout[0]=lout[1]=0; substit(l1); substit(l2);
  1279.   n=cutitems(l1,cut,MAX_LIST); if(n<=0) {*p=0; return;}
  1280.   for(i=0;i<n;i++) {
  1281.     ll=cut[i];
  1282.     if(ll[0] && itemchr(l2,ll)!=NULL && itemchr(lout,ll)==NULL &&
  1283.       strlen(lout)+strlen(ll)<MAX_LINELEN-2) {
  1284.       strcat(lout,",");strcat(lout,ll);
  1285.     }
  1286.   }
  1287.   ovlstrcpy(p,lout+1);
  1288. }
  1289.  
  1290. /* returns union of 2 lists */
  1291. void calc_listunion(char *p)
  1292. {
  1293.   char l1[MAX_LINELEN+1],l2[MAX_LINELEN+1],lout[MAX_LINELEN+2];
  1294.   char *cut[MAX_LIST];
  1295.   char *pp, *ll;
  1296.   int i,n;
  1297.  
  1298.   pp=wordchr(p,"and");
  1299.   if(pp==NULL) module_error("syntax_error");
  1300.   *pp=0;ovlstrcpy(l1,p); ovlstrcpy(l2,pp+strlen("and"));
  1301.   lout[0]=lout[1]=0; substit(l1); substit(l2);
  1302.   n=cutitems(l1,cut,MAX_LIST); for(i=0;i<n;i++) {
  1303.     ll=cut[i];
  1304.     if(ll[0] && itemchr(lout,ll)==NULL &&
  1305.         strlen(lout)+strlen(ll)<MAX_LINELEN-2) {
  1306.       strcat(lout,",");strcat(lout,ll);
  1307.     }
  1308.   }
  1309.   n=cutitems(l2,cut,MAX_LIST); for(i=0;i<n;i++) {
  1310.     ll=cut[i];
  1311.     if(ll[0] && itemchr(lout,ll)==NULL &&
  1312.         strlen(lout)+strlen(ll)<MAX_LINELEN-2) {
  1313.       strcat(lout,",");strcat(lout,ll);
  1314.     }
  1315.   }
  1316.   ovlstrcpy(p,lout+1);
  1317. }
  1318.  
  1319. /* returns items of list2 not in list1 */
  1320. void calc_listcomplement(char *p)
  1321. {
  1322.   char l1[MAX_LINELEN+1],l2[MAX_LINELEN+1],lout[MAX_LINELEN+2];
  1323.   char *cut[MAX_LIST];
  1324.   char *pp, *ll;
  1325.   int i,n;
  1326.  
  1327.   pp=wordchr(p,"in");
  1328.   if(pp==NULL) module_error("syntax_error");
  1329.   *pp=0;ovlstrcpy(l1,p); ovlstrcpy(l2,pp+strlen("in"));
  1330.   lout[0]=lout[1]=0; substit(l1); substit(l2);
  1331.   n=cutitems(l2,cut,MAX_LIST); if(n<=0) {*p=0; return;}
  1332.   for(i=0;i<n;i++) {
  1333.     ll=cut[i];
  1334.     if(ll[0] && itemchr(l1,ll)==NULL && itemchr(lout,ll)==NULL &&
  1335.         strlen(lout)+strlen(ll)<MAX_LINELEN-2) {
  1336.       strcat(lout,",");strcat(lout,ll);
  1337.     }
  1338.   }
  1339.   ovlstrcpy(p,lout+1);
  1340. }
  1341.  
  1342. /* Consult a dictionary to translate a string */
  1343. /*
  1344. void calc_dictionary(char *p)
  1345. {
  1346.   char *pp;
  1347.   char name[MAX_LINELEN+1], str[MAX_LINELEN+1];
  1348.   int t;
  1349.   pp=wordchr(p,"for");
  1350.   if(pp==NULL || pp<=p) module_error("syntax_error");
  1351.   *(find_word_end(pp-1))=0;
  1352.   ovlstrcpy(name,p); substit(name);
  1353.   pp=find_word_start(pp+strlen("for")); t=0;
  1354.   if(memcmp(pp,"word",strlen("word"))==0 &&
  1355.      (isspace(*(pp+strlen("word"))) ||
  1356.    (*(pp+strlen("word"))=='s' && isspace(*(pp+strlen("words")))))) {
  1357.    pp=find_word_start(pp+strlen("words")); t=1;
  1358.   }
  1359.   if(memcmp(pp,"line",strlen("line"))==0 &&
  1360.      (isspace(*(pp+strlen("line"))) ||
  1361.    (*(pp+strlen("line"))=='s' && isspace(*(pp+strlen("lines")))))) {
  1362.    pp=find_word_start(pp+strlen("lines")); t=1;
  1363.   }
  1364.   if(memcmp(pp,"list",strlen("list"))==0 && isspace(*(pp+strlen("list")))) {
  1365.    pp=find_word_start(pp+strlen("list"));
  1366.   }
  1367.   if(memcmp(pp,"items",strlen("items"))==0 && isspace(*(pp+strlen("items")))) {
  1368.    pp=find_word_start(pp+strlen("items"));
  1369.   }
  1370.   if(memcmp(pp,"item",strlen("item"))==0 && isspace(*(pp+strlen("item")))) {
  1371.    pp=find_word_start(pp+strlen("item"));
  1372.   }
  1373.   ovlstrcpy(str,pp); substit(str);
  1374.   switch(t) {
  1375.    case 1: {
  1376.        calc_words2items(str); break;
  1377.    }
  1378.    case 2: {
  1379.          calc_lines2items(str); break;
  1380.      }
  1381.      default: break;
  1382.     }
  1383. }
  1384. */
  1385.  
  1386. void calc_module(char *p)
  1387. {
  1388.   char *p1,*p2, ind_buf[MAX_LINELEN+1], buf[MAX_FNAME+1];
  1389.   char *tp;
  1390.  
  1391.   p1=find_word_start(p); p2=find_word_end(p1);
  1392.   if(*p2!=0) *p2++=0;
  1393.   p2=find_word_start(p2); *find_word_end(p2)=0;
  1394.   if(*p1==0) {empty: *p=0; return;}
  1395.   if(*p2==0) {
  1396.     snprintf(buf,sizeof(buf),"module_%s",p1);
  1397.     p1=getvar(buf); if(p1==NULL) p1="";
  1398.     mystrncpy(p,p1,MAX_LINELEN); return;
  1399.   }
  1400.   mkfname(buf,"%s/%s/INDEX",module_dir,p2);
  1401.   tp=readfile(buf,ind_buf,MAX_LINELEN);
  1402.   if(tp==NULL) {
  1403.     mkfname(buf,"%s/%s/index",module_dir,p2);
  1404.     tp=readfile(buf,ind_buf,MAX_LINELEN);
  1405.   }
  1406.   if(tp==NULL && p2[strlen(p2)-3]!='.') {
  1407.     mkfname(buf,"%s/%s.%s/INDEX",module_dir,p2,lang);
  1408.     tp=readfile(buf,ind_buf,MAX_LINELEN);
  1409.     if(tp==NULL) {
  1410.       mkfname(buf,"%s/%s.%s/index",module_dir,p2,lang);
  1411.       tp=readfile(buf,ind_buf,MAX_LINELEN);
  1412.     }
  1413.     if(tp==NULL) {
  1414.       int i;
  1415.       for(i=0;i<available_lang_no;i++) {
  1416.         mkfname(buf,"%s/%s.%s/INDEX",module_dir,p2,available_lang[i]);
  1417.         tp=readfile(buf,ind_buf,MAX_LINELEN); if(tp) break;
  1418.         mkfname(buf,"%s/%s.%s/index",module_dir,p2,
  1419.               available_lang[i]);
  1420.         tp=readfile(buf,ind_buf,MAX_LINELEN); if(tp) break;
  1421.       }
  1422.     }
  1423.   }
  1424.   if(tp==NULL) goto empty; /* module not found */
  1425.   _getdef(ind_buf,p1,p);
  1426. }
  1427.  
  1428. /* strip enclosing parentheses */
  1429. void calc_declosing(char *p)
  1430. {
  1431.   strip_enclosing_par(p);
  1432. }
  1433.  
  1434. /* remove html tag, very rudimentary. */
  1435. void calc_detag(char *p)
  1436. {
  1437.   char *p1, *p2;
  1438.   for(p1=strchr(p,'<'); p1!=NULL; p1=strchr(p1,'<')) {
  1439.     p2=strchr(p1,'>'); if(p2==NULL) p1++;
  1440.   else ovlstrcpy(p1,p2+1);
  1441.   }
  1442. }
  1443.  
  1444. /* replace html symbols by their iso translation */
  1445. void calc_html2iso(char *p)
  1446. {
  1447.   int st, ns;
  1448.   unsigned char buffer[MAX_LINELEN], *s = buffer;
  1449.   inithtmltrans();
  1450.   strcpy((char *)buffer, p);
  1451.   while (*s)
  1452.     if (*s == '&'){
  1453.         unsigned char *p1 = ++s;
  1454.         for (st = 0; (ns = htmltrans[abs(st)][*s]); st = ns)
  1455.            s++;
  1456.         if (st > 0 && *s == ';')
  1457.           {char *r=htmlsymbs[st][1]; strcpy((char *)p, r); s++; p+=strlen(r);}
  1458.         else
  1459.           {*p++='&'; s=p1;}
  1460.       }
  1461.     else *p++=*s++;
  1462.   *p = 0;
  1463. }
  1464.  
  1465. /* prepare a string to be inserted into a form input or textarea as is */
  1466. void calc_reinput(char *p)
  1467. {
  1468.   char *p1;
  1469.   for(p1=strchr(p,'&'); p1!=NULL; p1=strchr(p1,'&')) {
  1470.     p1++; string_modify(p,p1,p1,"amp;");
  1471.   }
  1472.   for(p1=strchr(p,'<'); p1!=NULL; p1=strchr(++p1,'<'))
  1473.     string_modify(p,p1,p1+1,"&lt;");
  1474. }
  1475.  
  1476. /* get a definition from a file. Trusted modules only. */
  1477. void calc_defof(char *p)
  1478. {
  1479.   char *p1;
  1480.   char fbuf[MAX_FNAME+1], nbuf[MAX_LINELEN+1], tbuf[MAX_LINELEN+1];
  1481.  
  1482.   secure_exec();
  1483.   p1=wordchr(p,"in"); if(p1==NULL) module_error("syntax_error");
  1484.   *p1=0; p1=find_word_start(p1+strlen("in"));
  1485.   mystrncpy(nbuf,p,sizeof(nbuf));  mystrncpy(tbuf,p1,sizeof(tbuf));
  1486.   substit(nbuf); substit(tbuf);
  1487.   p1=find_word_start(tbuf); *find_word_end(p1)=0;
  1488.   if(find_module_file(p1,fbuf,0)) {*p=0; return;}
  1489.   p1=find_word_start(nbuf); strip_trailing_spaces(p1);
  1490.   getdef(fbuf,p1,p);
  1491. }
  1492.  
  1493. /* check host */
  1494. void calc_checkhost(char *p)
  1495. {
  1496.   int t;
  1497.   if(robot_access || !trusted_module()) ovlstrcpy(p,"0");
  1498.   else {
  1499.     t=checkhost(p); mystrncpy(p,int2str(t),MAX_LINELEN);
  1500.   }
  1501. }
  1502.  
  1503. #define MAX_COLUMNS 256
  1504.  
  1505. /* A rudimentary database facility */
  1506. void calc_select(char *p)
  1507. {
  1508.   char *p1, *p2, *p3, *bufp, c, sep;
  1509.   char buf1[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  1510.   char buf[MAX_LINELEN+1];
  1511.   int i,j,lmax;
  1512.   p2=wordchr(p,"where"); if(p2==NULL) module_error("syntax_error");
  1513.   *p2=0; p2+=strlen("where");
  1514.   p2=find_word_start(p2); strip_trailing_spaces(p2);
  1515.   p1=find_word_start(p) ; strip_trailing_spaces(p1);
  1516.   if(*p1==0 || *p2==0) module_error("syntax_error");
  1517.   ovlstrcpy(buf1,p1); ovlstrcpy(buf2,p2); sep='\n';
  1518. /* buf1: data; buf2: condition. */
  1519.   substit(buf1);
  1520.   if(strstr(buf2,"column")!=NULL) { /* matrix */
  1521.     lmax=0; if(rows2lines(buf1)) sep=';';
  1522.     for(p1=strstr(buf2,"column"); p1; p1=strstr(p1+1,"column")) {
  1523.       if(p1>buf2 && isalnum(*(p1-1))) continue;
  1524.       p2=find_word_start(p1+strlen("column"));
  1525.       for(p3=p2; myisdigit(*p3); p3++);
  1526.       if(p3==p2) continue;
  1527.       c=*p3; *p3=0; i=atoi(p2); *p3=c;
  1528.       if(i<=0 || i>MAX_COLUMNS) continue;
  1529.       if(i>lmax) lmax=i;
  1530.       string_modify(buf2,p1,p3,"$(wims_select_col_%d)",i);
  1531.     }
  1532.     buf[0]=0; bufp=buf;
  1533.     for(p1=buf1; *p1; p1=p2) {
  1534.       char ibuf[MAX_LINELEN+1], nbuf[MAX_NAMELEN+1];
  1535.       p2=strchr(p1,'\n');
  1536.       if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
  1537.       if(*find_word_start(p1)==0) continue;
  1538.       for(j=1;j<=lmax;j++) {
  1539.         snprintf(nbuf,sizeof(nbuf),"wims_select_col_%d",j);
  1540.         fnd_item(p1,j,ibuf); force_setvar(nbuf,ibuf);
  1541.       }
  1542.       ovlstrcpy(ibuf,buf2); if(compare(ibuf,0,0)) {
  1543.         snprintf(bufp,MAX_LINELEN-(bufp-buf),"%s%c",p1,sep);
  1544.         bufp+=strlen(bufp);
  1545.       }
  1546.     }
  1547.   }
  1548.   else { /* datafile */
  1549.    module_error("syntax_error");
  1550.   }
  1551.   if(buf[0]!=0) buf[strlen(buf)-1]=0;
  1552.   mystrncpy(p,buf,MAX_LINELEN);
  1553. }
  1554.  
  1555. /* Extract a column from a matrix */
  1556. void calc_columnof(char *p)
  1557. {
  1558.   char *p1, *p2, *bufp, sep;
  1559.   char buf1[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  1560.   char buf[MAX_LINELEN+1];
  1561.   p2=wordchr(p,"of"); if(p2==NULL) module_error("syntax_error");
  1562.   *p2=0; p2+=strlen("of");
  1563.   p2=find_word_start(p2); strip_trailing_spaces(p2);
  1564.   p1=find_word_start(p) ; strip_trailing_spaces(p1);
  1565.   if(*p1==0) module_error("syntax_error");
  1566.   if(*p2==0) {*p=0; return;}
  1567.   ovlstrcpy(buf1,p1); ovlstrcpy(buf2,p2); sep='\n';
  1568. /* buf1: number(s); buf2: matrix. */
  1569.   substit(buf1); substit(buf2);
  1570.   if(rows2lines(buf2)) sep=';';
  1571.   if(strchr(buf1,',')==NULL && wordchr(buf1,"to")==NULL
  1572.      && strstr(buf1,"..")==NULL) sep=',';
  1573.   buf[0]=0; bufp=buf;
  1574.   for(p1=buf2; *p1; p1=p2) {
  1575.     char ibuf[MAX_LINELEN+1];
  1576.     p2=strchr(p1,'\n');
  1577.     if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
  1578.     snprintf(ibuf,sizeof(ibuf),"%s of %s",buf1,p1);
  1579.     calc_itemof(ibuf);
  1580.     snprintf(bufp,MAX_LINELEN-(bufp-buf),"%s%c",ibuf,sep);
  1581.     bufp+=strlen(bufp);
  1582.   }
  1583.   if(buf[0]!=0) buf[strlen(buf)-1]=0;
  1584.   mystrncpy(p,buf,MAX_LINELEN);
  1585. }
  1586.  
  1587. /* find roots of a function or equation in a given zone */
  1588. void calc_solve(char *p)
  1589. {
  1590.   char *pp, *fp, *forp;
  1591.   char buf[MAX_LINELEN+1], vbuf[MAX_LINELEN+1];
  1592.   double v, dd, start, stop, step, old, v1, v2, v3, d1, d3;
  1593.   int i, pos;
  1594.  
  1595.   forp=wordchr(p,"for");
  1596.   if(forp==NULL) { syntax: module_error("syntax_error"); *p=0; return; }
  1597.   *forp=0; forp+=strlen("for");
  1598.   fp=find_word_start(p); strip_trailing_spaces(fp);
  1599.   if(*fp==0) goto syntax;
  1600.   i=cutfor(forp,NULL); if(i<0 || forstruct.type==for_in) goto syntax;
  1601.   if(i>0) {*p=0; return;}
  1602.   start=forstruct.from; stop=forstruct.to; forp=forstruct.var;
  1603.   mystrncpy(buf,fp,sizeof(buf)); substitute(buf);
  1604.   *p=0; pp=strchr(buf,'='); if(pp!=NULL) {
  1605.     if(strlen(buf)>=MAX_LINELEN-16) return;
  1606.     strcat(buf,")");
  1607.     string_modify(buf,pp,pp+1,"-(");
  1608.   }
  1609.   i=0; for(fp=varchr(buf,forp); fp!=NULL; fp=varchr(fp,forp)) {
  1610.     string_modify(buf,fp,fp+strlen(forp),EV_X); fp+=strlen(EV_X); i++;
  1611.   }
  1612.   if(i==0 || start==stop) return;
  1613.   evalue_compile(buf); pos=eval_getpos(EV_X);
  1614.   if(start>stop) {dd=start; start=stop; stop=dd;}
  1615.   step=(stop-start)/100; if(step==0) return;
  1616.   pp=p; old=0;
  1617.   for(v=start; v<=stop; v+=step, old=dd) {
  1618.     eval_setval(pos,v);
  1619.     dd=checked_eval(buf);
  1620.     if(v==start) continue;
  1621.     if(!isfinite(old) || !isfinite(dd) || (old>0 && dd>0) || (old<0 && dd<0))
  1622.       continue;
  1623.     if(dd==0 && v<stop) continue;
  1624.     v1=v-step; v2=v; d1=old;
  1625.     for(i=0;i<30;i++) {
  1626.       v3=(v1+v2)/2; eval_setval(pos,v3);
  1627.       d3=checked_eval(buf);
  1628.       if(!isfinite(d3)) goto next;
  1629.       if((d1>0 && d3>0) || (d1<0 && d3<0)) {d1=d3; v1=v3;}
  1630.       else {v2=v3;}
  1631.     }
  1632.     float2str(v3,vbuf); if(pp-p+strlen(vbuf)<MAX_LINELEN-1) {
  1633.       if(pp>p) *pp++=',';
  1634.       ovlstrcpy(pp,vbuf);
  1635.       pp+=strlen(pp);
  1636.     }
  1637.     else break;
  1638.     next: ;
  1639.   }
  1640. }
  1641.  
  1642. /* type: 1=values, 2=sum, 3=product, 4=recursion, 5=subst */
  1643. void _values(char *p, int type)
  1644. {
  1645.   char *pp, *fp, *forp;
  1646.   char vbuf[MAX_LINELEN+1], buf[MAX_LINELEN+1], fbuf[MAX_LINELEN+1], tbuf[MAX_LINELEN+1];
  1647.   char *f[64];
  1648.   double v, dd, start, stop, step, v0;
  1649.   int i, k, fcnt, pos, posr;
  1650.  
  1651.   forp=wordchr(p,"for");
  1652.   if(forp==NULL) { syntax: module_error("syntax_error"); *p=0; return; }
  1653.   *forp=0; forp+=strlen("for"); forp=find_word_start(forp);
  1654.   fp=find_word_start(p); strip_trailing_spaces(fp);
  1655.   if(*fp==0) goto syntax;
  1656.   if(type<5) i=cutfor(forp,NULL); else i=cutfor(forp,tbuf);
  1657.   if(i<0) goto syntax;
  1658.   if(i>0) {*p=0; return;}
  1659.   start=forstruct.from; stop=forstruct.to; step=forstruct.step;
  1660.   forp=forstruct.var;
  1661.   mystrncpy(buf,fp,sizeof(buf)); substitute(buf);
  1662.   for(fp=varchr(buf,forp); fp!=NULL; fp=varchr(fp,forp)) {
  1663.     string_modify(buf,fp,fp+strlen(forp),EV_X); fp+=strlen(EV_X);
  1664.   }
  1665.   for(fp=varchr(buf,"last"); fp!=NULL; fp=varchr(fp,"last")) {
  1666.     string_modify(buf,fp,fp+strlen("last"),EV_S); fp+=strlen(EV_S);
  1667.   }
  1668.   fcnt=pos=posr=0;
  1669.   if(type==5) goto skip;
  1670.   fcnt=itemnum(buf); if(fcnt>64) fcnt=64; pp=fbuf;
  1671.   for(k=0; k<fcnt; k++) {
  1672.     fnd_item(buf,k+1,vbuf); evalue_compile(vbuf);
  1673.     if(pp-fbuf+strlen(vbuf)<MAX_LINELEN-1) {
  1674.       f[k]=pp; ovlstrcpy(pp,vbuf); pp+=strlen(pp)+1;
  1675.     }
  1676.     else f[k]="";
  1677.   }
  1678.   pos=eval_getpos(EV_X); posr=eval_getpos(EV_S);
  1679.   skip:
  1680.   if(step==0) step=1;/* if(step<0) step=-step;
  1681.   if(stop<start) {dd=start; start=stop; stop=dd;} */
  1682.   *p=0; v0=0;
  1683.   switch(type) {
  1684.     case 4:
  1685.     case 1: {
  1686.       pp=getvar("recursion_start");
  1687.       if(pp==NULL || *pp==0) v0=0;
  1688.       else {
  1689.         v0=evalue(pp); if(!isfinite(v0)) return;
  1690.       }
  1691.       break;
  1692.     }
  1693.     case 2: v0=0; break;
  1694.     case 3: v0=1; break;
  1695.     case 5: break;
  1696.     }
  1697.     pp=p;
  1698.     if(type==5) {
  1699.       char *ps, *pt, buf2[MAX_LINELEN+1];
  1700.       int l,ln;
  1701.       *p=0; l=strlen(buf); if(l>=MAX_LINELEN) return;
  1702.       for(i=0,v=start; i<MAX_VALUE_LIST && v*step<=stop*step; v+=step, i++) {
  1703.         if(forstruct.type==for_from) {
  1704.           float2str(v,vbuf); ps=vbuf;
  1705.         }
  1706.         else ps=forstruct.pos[i];
  1707.         ovlstrcpy(buf2,buf); l=strlen(ps);ln=strlen(EV_X);
  1708.         for(pt=varchr(buf2,EV_X);pt!=NULL;pt=varchr(pt+l,EV_X))
  1709.           string_modify(buf2,pt,pt+ln,"%s",ps);
  1710.         if(pp-p+strlen(buf2)>=MAX_LINELEN-1) return;
  1711.         if(pp>p) *pp++=',';
  1712.         ovlstrcpy(pp,buf2);
  1713.         pp+=strlen(pp);
  1714.       }
  1715.       return;
  1716.     }
  1717.     for(i=0,v=start; i<MAX_VALUE_LIST && v*step<=stop*step; v+=step, i++) {
  1718.       if(forstruct.type==for_from) eval_setval(pos,v);
  1719.       else eval_setval(pos,forstruct.list[i]);
  1720.       eval_setval(posr,v0);
  1721.       for(k=0; k<fcnt; k++) {
  1722.         dd=checked_eval(f[k]);
  1723.         switch(type) {
  1724.           case 1: { /* values */
  1725.             float2str(dd,vbuf);
  1726.             if(pp-p+strlen(vbuf)<MAX_LINELEN-1) {
  1727.               if(pp>p) *pp++=',';
  1728.               ovlstrcpy(pp,vbuf);
  1729.               pp+=strlen(pp);
  1730.             }
  1731.             v0=dd; break;
  1732.          }
  1733.          case 2: { /* sum */
  1734.           v0=v0+dd; break;
  1735.         }
  1736.         case 3: { /* product */
  1737.           v0=v0*dd; break;
  1738.         }
  1739.         case 4: { /* recursion */
  1740.           v0=dd; break;
  1741.         }
  1742.       }
  1743.     }
  1744.   }
  1745.   if(type!=1) float2str(v0,p);
  1746. }
  1747.  
  1748. /* cut a function into values */
  1749. void calc_values(char *p)
  1750. { _values(p,1); }
  1751.  
  1752. /* compute sum */
  1753. void calc_sum(char *p)
  1754. { _values(p,2); }
  1755.  
  1756. /* compute product */
  1757. void calc_product(char *p)
  1758. { _values(p,3); }
  1759.  
  1760. /* simple recursion */
  1761. void calc_recursion(char *p)
  1762. { _values(p,4); }
  1763.  
  1764. /* List substitution */
  1765. void calc_makelist(char *p)
  1766. { _values(p,5); }
  1767.  
  1768. /* level curve data */
  1769. void calc_leveldata(char *p)
  1770. {
  1771.   leveldata ld;
  1772.   char *sizep, *rangep, *fp, *levelp, *stepp;
  1773.   char *pp,*p2,fbuf[MAX_LINELEN+1],buf[MAX_LINELEN+1];
  1774.   double d[4];
  1775.   int i;
  1776.  
  1777.   sizep=wordchr(p,"size");
  1778.   rangep=wordchr(p,"ranges");
  1779.   fp=wordchr(p,"function");
  1780.   levelp=wordchr(p,"levels");
  1781.   stepp=wordchr(p,"step");
  1782.   if(sizep==NULL || rangep==NULL || fp==NULL) {
  1783.     syntax: module_error("syntax_error"); *p=0; return;
  1784.   }
  1785.   *sizep=0; sizep+=strlen("size");
  1786.   *rangep=0; rangep+=strlen("ranges");
  1787.   *fp=0; fp+=strlen("function");
  1788.   if(levelp!=NULL) {*levelp=0; levelp+=strlen("levels");}
  1789.   else levelp="0";
  1790.   if(stepp!=NULL) {*stepp=0; stepp+=strlen("step");}
  1791.   else stepp="0";
  1792.   mystrncpy(fbuf,fp,sizeof(fbuf)); substitute(fbuf);
  1793.   ld.fn=fbuf;
  1794.   ld.xname="x"; ld.yname="y"; ld.grain=evalue(stepp);
  1795.   mystrncpy(buf,sizep,sizeof(buf)); substitute(buf);
  1796.   for(i=0,pp=buf;i<2;i++,pp=p2) {
  1797.     if(*pp==0) goto syntax;
  1798.     p2=find_item_end(pp); if(*p2) *p2++=0;
  1799.     d[i]=evalue(pp);
  1800.   }
  1801.   ld.xsize=d[0]; ld.ysize=d[1];
  1802.   mystrncpy(buf,rangep,sizeof(buf)); substitute(buf);
  1803.   for(i=0,pp=buf;i<4;i++,pp=p2) {
  1804.     if(*pp==0) goto syntax;
  1805.     p2=find_item_end(pp); if(*p2) *p2++=0;
  1806.     d[i]=evalue(pp);
  1807.   }
  1808.   ld.xrange[0]=d[0]; ld.xrange[1]=d[1]; ld.yrange[0]=d[3]; ld.yrange[1]=d[2];
  1809.   mystrncpy(buf,levelp,sizeof(buf)); substitute(buf);
  1810.   ld.levelcnt=itemnum(buf); if(ld.levelcnt>LEVEL_LIM) ld.levelcnt=LEVEL_LIM;
  1811.   for(i=0,pp=buf;i<ld.levelcnt;i++,pp=p2) {
  1812.     if(*pp==0) goto syntax;
  1813.     p2=find_item_end(pp); if(*p2) *p2++=0;
  1814.     ld.levels[i]=evalue(pp);
  1815.   }
  1816.   levelcurve(&ld);
  1817.   for(i=0, pp=p; i<ld.datacnt && pp<p+MAX_LINELEN-16; i++) {
  1818.     float2str(ld.xdata[i],buf);
  1819.     if(pp-p+strlen(buf)<MAX_LINELEN-1) {
  1820.       if(pp>p) *pp++=';';
  1821.       ovlstrcpy(pp,buf); pp+=strlen(pp);
  1822.     }
  1823.     float2str(ld.ydata[i],buf);
  1824.     if(pp-p+strlen(buf)<MAX_LINELEN-1) {
  1825.       if(pp>p) *pp++=',';
  1826.       ovlstrcpy(pp,buf); pp+=strlen(pp);
  1827.     }
  1828.   }
  1829. }
  1830.  
  1831. /* internal routine with no security check */
  1832. void _lookup(char *p, char *fname)
  1833. {
  1834.   char buf1[MAX_LINELEN+1];
  1835.   char *p1, *p2, *mbuf;
  1836.  
  1837.   mbuf=readfile(fname,NULL,WORKFILE_LIMIT);
  1838.   if(mbuf==NULL) {abort: *p=0; return;}
  1839.   p1=find_word_start(p); strip_trailing_spaces(p1);
  1840.   snprintf(buf1,sizeof(buf1),"%s:",p1); substit(buf1);
  1841.   for(p1=strstr(mbuf,buf1);
  1842.   p1!=NULL && p1>mbuf && *(p1-1)!='\n';
  1843.   p1=strstr(p1+1,buf1));
  1844.   if(p1==NULL) {free(mbuf); goto abort;}
  1845.   p1+=strlen(buf1);
  1846.   for(p2=strchr(p1,'\n'); p2!=NULL; p2=strchr(p2+1,'\n')) {
  1847.     if(p2>p1 && *(p2-1)=='\\') {*(p2-1)=' '; continue;}
  1848.     else break;
  1849.   }
  1850.   if(p2==NULL) p2=p1+strlen(p1); else *p2=0;
  1851.   mystrncpy(p,p1,MAX_LINELEN); free(mbuf);
  1852. }
  1853.  
  1854. /* lookup a definition in a definition file */
  1855. void calc_lookup(char *p)
  1856. {
  1857.   char buf2[MAX_LINELEN+1], buf3[MAX_FNAME+1];
  1858.   char *p2;
  1859.  
  1860.   p2=wordchr(p,"in"); if(p2==NULL) {abort: *p=0; return;}
  1861.   *p2=0;p2=find_word_start(p2+2);
  1862.   mystrncpy(buf2,p2,sizeof(buf2)); substit(buf2);
  1863.   *find_word_end(buf2)=0;
  1864.   if(strstr(buf2,parent_dir_string)!=NULL) {
  1865.     force_setvar("wims_error_data",buf2); module_error("illegal_cmd");
  1866.   }
  1867.   if(strncmp(buf2,"bases/",strlen("bases/"))!=0) {
  1868.     if(datafile_check(buf2)!=0 || find_module_file(buf2,buf3,0)!=0)
  1869.       goto abort;
  1870.     _lookup(p,buf3);
  1871.   }
  1872.   else _lookup(p,buf2);
  1873. }
  1874.  
  1875. /* Hide name of a file. Only in module directory or in gifs/ */
  1876. void calc_rename(char *p)
  1877. {
  1878.   char buf1[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  1879.   char *p1, *ext, *s;
  1880.   int t;
  1881.  
  1882.   if(robot_access || strstr(p,"getfile")!=NULL) return;
  1883.   p1=find_word_start(p); *find_word_end(p1)=0;
  1884.   if(strncmp(p1,ref_name,strlen(ref_name))==0) p1+=strlen(ref_name);
  1885.   if(p1>p) ovlstrcpy(p,p1);
  1886.   if(strstr(p,parent_dir_string)!=NULL ||
  1887.      strncmp(p,"modules/adm/",strlen("modules/adm/"))==0) {
  1888.     badfile: force_setvar("wims_error_data",p); module_error("illegal_cmd");
  1889.   }
  1890.   if(strncmp(p,module_dir,strlen(module_dir))!=0 &&
  1891.      strncmp(p,"modules/data/",strlen("modules/data/"))!=0 &&
  1892.      strncmp(p,"scripts/data/",strlen("scripts/data/"))!=0 &&
  1893.      strncmp(p,"gifs",strlen("gifs"))!=0) goto badfile;
  1894.   mkfname(buf1,"%s/getfile",session_prefix); mkdirs(buf1);
  1895.   mkfname(buf1,"%s/.rename",session_prefix);
  1896.   mystrncpy(buf2,p,sizeof(buf2)); _lookup(buf2,buf1);
  1897.   if(buf2[0]!=0) { /* already */
  1898.     mystrncpy(p,buf2,MAX_LINELEN); return;
  1899.   }
  1900.   if(cwdbuf[0]==0) return;
  1901.   p1=p+strlen(p)-1;
  1902.   while(p1>p && isalnum(*p1)) p1--;
  1903.   if(p1>p && *p1=='.') ext=p1; else ext="";
  1904.   rerand: t=random();
  1905.   mkfname(buf1,"%s/%s",cwdbuf,p);
  1906.   mkfname(buf2,"%s/getfile/rename-%u%s",session_prefix,t,ext);
  1907.   if(ftest(buf2)>=0) goto rerand;
  1908.   (void) symlink(buf1,buf2);
  1909.   s=getvar("wims_session"); if(s==NULL) return;
  1910.   if(good_httpd) snprintf(buf1,sizeof(buf1),
  1911.                    "getfile/rename-%u%s?session=%s", t,ext,s);
  1912.   else snprintf(buf1,sizeof(buf1),"%s?cmd=getfile&+session=%s&+special_parm=rename-%u%s",
  1913.             ref_name, s, t,ext);
  1914.   snprintf(buf2,sizeof(buf2),"%s:%s\n",p,buf1);
  1915.   accessfile(buf2,"a","%s/.rename",session_prefix);
  1916.   mystrncpy(p,buf1,MAX_LINELEN);
  1917. }
  1918.  
  1919. /* Pick up and translate imgrename(...) within a string */
  1920. void calc_imgrename(char *p)
  1921. {
  1922.   char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  1923.   char *p1, *p2, *p3, *p4;
  1924.  
  1925.   for(p1=varchr(p,"imgrename"); p1!=NULL; p1=varchr(p1,"imgrename")) {
  1926.     p2=find_word_start(p1+strlen("imgrename"));
  1927.     if(*p2!='(') {p1=p2; continue;}
  1928.     p2++; p3=find_matching(p2,')');
  1929.     if(*p3!=')') {p1=p2-1; continue;}
  1930.     p2=find_word_start(p2); p4=find_word_end(p2);
  1931.      memmove(buf,p2,p4-p2); buf[p4-p2]=0;
  1932.     calc_rename(buf); *p3=0;
  1933.     snprintf(buf2,sizeof(buf2),"<img src=\"%s\"%s alt=\"\" />",buf, p4);
  1934.     *p3=')'; p3++;
  1935.     string_modify(p,p1,p3,"%s",buf2);
  1936.       p1+=strlen(buf2);
  1937.   }
  1938. }
  1939.  
  1940. /* internal use only */
  1941. void calc_unhttp(char *p)
  1942. {
  1943.   _http2env(tmplbuf,p); mystrncpy(p,tmplbuf,MAX_LINELEN);
  1944. }
  1945.  
  1946. /*
  1947. char *saltchar="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
  1948. */
  1949.  
  1950. void calc_passcheck(char *p)
  1951. {
  1952.  
  1953. }
  1954.  
  1955. void calc_passcrypt(char *p)
  1956. {
  1957. #ifdef HAVE_CRYPT
  1958.   char saltstr[4];
  1959.   char *p1, *p2, *pp, *s, buf[MAX_LINELEN+1];
  1960.   saltstr[0]='N'; saltstr[1]='v'; saltstr[2]=0; buf[0]=0; pp=buf;
  1961.   for(p1=find_word_start(p);*p1;p1=find_word_start(p2)) {
  1962.     p2=find_word_end(p1); if(*p2) *p2++=0;
  1963.     pp=pp+strlen(pp);
  1964.     if(pp>buf) s=" "; else s="";
  1965.     if(*p1=='*')
  1966.       snprintf(pp,MAX_LINELEN-(pp-buf),"%s%s",s,p1);
  1967.     else
  1968.       snprintf(pp,MAX_LINELEN-(pp-buf),"%s*%s",s,crypt(p1,saltstr));
  1969.   }
  1970.   ovlstrcpy(p,buf);
  1971. #endif
  1972. }
  1973.  
  1974. void exec_readproc(char *p);
  1975.  
  1976. /* crypted mail interface */
  1977. void calc_mailurl(char *p)
  1978. {
  1979.   char *p0, buf[MAX_LINELEN+1];
  1980.  
  1981.   if(robot_access) {*p=0; return;}
  1982.   snprintf(buf,sizeof(buf),"mailurl.proc %s",p);
  1983.   exec_readproc(buf);
  1984.   p0=getvar("mailurl_"); if(p0==NULL) p0="";
  1985.   mystrncpy(p,p0,MAX_LINELEN);
  1986. }
  1987.  
  1988. /* get option word in a string */
  1989. void calc_getopt(char *p)
  1990. {
  1991.   char *p1, *p2, *p3, *p4;
  1992.   char buf1[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  1993.  
  1994.   p1=wordchr(p,"in"); if(p1==NULL) module_error("syntax error");
  1995.   *p1=0; p1=find_word_start(p1+3);
  1996.   mystrncpy(buf1,p,MAX_LINELEN); mystrncpy(buf2,p1,MAX_LINELEN);
  1997.   substitute(buf1); substitute(buf2);
  1998.   p1=find_word_start(buf1); *find_word_end(p1)=0;
  1999.   for(p2=buf2;*p2;p2++) {
  2000.     if(myisspace(*p2)) *p2=' ';
  2001.     if(*p2=='=') *p2='  ';
  2002.   }
  2003.   *p=0;
  2004.   p2=wordchr(buf2,p1); if(p2==NULL) return;
  2005.   for(p3=find_word_end(p2);myisspace(*p3);p3++) {
  2006.     if(*p3=='   ') {
  2007.       p3=find_word_start(p3);
  2008.       switch(*p3) {
  2009.       case '"': {
  2010.         p4=strchr(p3+1,'"');
  2011.         goto tested;
  2012.       }
  2013.       case '(': {
  2014.         p4=find_matching(p3+1,')');
  2015.         goto tested;
  2016.       }
  2017.       case '[': {
  2018.         p4=find_matching(p3+1,']');
  2019.         goto tested;
  2020.       }
  2021.       case '{': {
  2022.         p4=find_matching(p3+1,'}');
  2023.         tested:
  2024.         if(p4) {
  2025.           p3++; *p4=0; break;
  2026.         }
  2027.         else goto nomatch;
  2028.       }
  2029.       default: {
  2030.         nomatch:
  2031.         *find_word_end(p3)=0;
  2032.         }
  2033.       }
  2034.       mystrncpy(p,p3,MAX_LINELEN);
  2035.       return;
  2036.     }
  2037.   }
  2038.   *find_word_end(p2)=0;
  2039.   mystrncpy(p,p2,MAX_LINELEN);
  2040. }
  2041.  
  2042. /* internal */
  2043. void _embraced(char buf[], void (app)(char *p))
  2044. {
  2045.   char *p1, *p2, buf2[MAX_LINELEN+1];
  2046.   for(p1=strchr(buf,'{'); p1; p1=strchr(p1,'{')) {
  2047.     p2=find_matching(p1+1,'}');
  2048.     if(p2==NULL) module_error("unmatched_parentheses");
  2049.     *p2=0; mystrncpy(buf2,p1+1,sizeof(buf2)); app(buf2);
  2050.     string_modify(buf,p1,p2+1,buf2);
  2051.   }
  2052. }
  2053.  
  2054. /* embraced operations */
  2055. void calc_embraced(char *p)
  2056. {
  2057.   char *p1, *p2, buf[MAX_LINELEN+1];
  2058.  
  2059.   p1=find_word_start(p); p2=find_word_end(p1);
  2060.   if(*p2==0) {*p=0; return;}
  2061.   *p2++=0; p2=find_word_start(p2);
  2062.   mystrncpy(buf,p2,sizeof(buf)); substit(buf);
  2063.   if(p1>p) ovlstrcpy(p,p1);
  2064.   substit(p);
  2065.   if(strcmp(p,"randitem")==0) {_embraced(buf,calc_randitem); goto end;}
  2066.   if(strcmp(p,"extract")==0) {
  2067.     p1=strchr(buf,'{');
  2068.     if(p1!=NULL) {
  2069.       p2=find_matching(++p1,'}');
  2070.       if(p2!=NULL) {
  2071.         memmove(buf,p1,p2-p1); buf[p2-p1]=0;
  2072.       }
  2073.       else buf[0]=0;
  2074.     }
  2075.     else buf[0]=0;
  2076.     goto end;
  2077.   }
  2078.   if(strcmp(p,"delete")==0) {
  2079.     for(p1=strchr(buf,'{'); p1; p1=strchr(p1,'{')) {
  2080.       p2=find_matching(p1+1,'}');
  2081.       if(p2) ovlstrcpy(p1,p2+1); else p1++;
  2082.     }
  2083.     goto end;
  2084.   }
  2085.   module_error("syntax_error");
  2086.  
  2087.   end:
  2088.   mystrncpy(p,buf,MAX_LINELEN);
  2089. }
  2090.  
  2091. void calc_rows2lines(char *p)
  2092. { rows2lines(p); }
  2093.  
  2094. void calc_lines2rows(char *p)
  2095. { lines2rows(p); }
  2096.  
  2097. /* check whether datamodules exist */
  2098. void calc_checkdata(char *p)
  2099. {
  2100.   char *p1, *p2, buf[MAX_LINELEN+1], nbuf[MAX_FNAME+1];
  2101.   struct stat st;
  2102.  
  2103.   memmove(p,"yes",4);
  2104.   p1=getvar("module_data");
  2105.   if(p1==NULL || *p1==0) return;
  2106.   snprintf(buf,sizeof(buf),"%s",p1);
  2107.   for(p2=buf; *p2; p2++) if(*p2==',' || *p2==';') *p2=' ';
  2108.   if(strstr(buf,"..")!=NULL) {
  2109.     snprintf(p,MAX_LINELEN,"badly_defined_data_module");
  2110.     return;
  2111.   }
  2112.   for(p1=find_word_start(buf); *p1; p1=find_word_start(p2)) {
  2113.     p2=find_word_end(p1); if(*p2) *p2++=0;
  2114.     snprintf(nbuf,sizeof(nbuf),"%s/%s/INDEX",module_dir,p1);
  2115.     if(stat(nbuf,&st)<0) {
  2116.       snprintf(nbuf,sizeof(nbuf),"%s/%s/index",module_dir,p1);
  2117.       if(stat(nbuf,&st)<0) {
  2118.         snprintf(p,MAX_LINELEN,"%s",p1);
  2119.         return;
  2120.       }
  2121.     }
  2122.   }
  2123. }
  2124.  
  2125. /* tag!=0 if we don't want automatic substit(). */
  2126. MYFUNCTION calc_routine[]={
  2127.       {"TeXmath", 0, texmath},
  2128.       {"add",  1, calc_sum},
  2129.       {"append", 1, calc_append},
  2130.       {"call",   0, calc_exec},
  2131.       {"char",  1, calc_charof},
  2132.       {"charcnt", 0,      calc_lengthof},
  2133.       {"charcount", 0,      calc_lengthof},
  2134.       {"charno", 0,      calc_lengthof},
  2135.       {"charnum", 0,      calc_lengthof},
  2136.       {"chars",  1, calc_charof},
  2137.       {"checkdata", 0, calc_checkdata},
  2138.       {"checkdatamodule",0, calc_checkdata},
  2139.       {"checkhost", 0, calc_checkhost},
  2140.       {"column", 1, calc_columnof},
  2141.       {"columns", 1, calc_columnof},
  2142.       {"daemon", 0, calc_daemon},
  2143.       {"date",  0, calc_date},
  2144.       {"deaccent", 0, deaccent},
  2145.       {"debug",  0, calc_debug},
  2146.       {"declosing", 0, calc_declosing},
  2147.       {"definitionof", 1, calc_defof},
  2148.       {"defof",  1, calc_defof},
  2149.       {"detag",  0, calc_detag},
  2150. /*      {"dictionary", 1, calc_dictionary}, */
  2151.       {"dir",  0, calc_listfile},
  2152.       {"embraced", 1, calc_embraced},
  2153.       {"encyclo", 0, pedia},
  2154.       {"encyclopedia", 0, pedia},
  2155.       {"eval",  0, calc_evalue},
  2156.       {"evalsubst", 1, calc_evalsubst},
  2157.       {"evalsubstit", 1, calc_evalsubst},
  2158.       {"evalsubstitute",1, calc_evalsubst},
  2159.       {"evalue", 0, calc_evalue},
  2160.       {"evaluesubst", 1, calc_evalsubst},
  2161.       {"evaluesubstit", 1, calc_evalsubst},
  2162.       {"evaluesubstitute",1, calc_evalsubst},
  2163.       {"examdep", 0, calc_examdep},
  2164.       {"examscore", 0, calc_examscore},
  2165.       {"exec",   0, calc_exec},
  2166.       {"execute", 0, calc_exec},
  2167.       {"fileexists", 0, calc_fileexists},
  2168.       {"filelist", 0, calc_listfile},
  2169.       {"filexists", 0, calc_fileexists},
  2170.       {"getdef", 1, calc_defof},
  2171.       {"getopt", 1, calc_getopt},
  2172.       {"getscore", 0, calc_getscore},
  2173.       {"getscorealltries", 0, calc_getscorealltries},
  2174.       {"getscorebest", 0, calc_getscorebest},
  2175.       {"getscorelast", 0, calc_getscorelast},
  2176.       {"getscorelevel", 0, calc_getscorelevel},
  2177.       {"getscoremaxexotry",0,calc_getscoremaxexotry},
  2178.       {"getscoremean",0, calc_getscoremean},
  2179.       {"getscorepercent",0, calc_getscorepercent},
  2180.       {"getscorequality",0, calc_getscoremean},
  2181.       {"getscoreremain",0, calc_getscoreremain},
  2182.       {"getscorerequire",0, calc_getscorerequire},
  2183.       {"getscorestatus",0, calc_getscorestatus},
  2184.       {"getscoretry", 0, calc_getscoretry},
  2185.       {"getscoreweight",0, calc_getscoreweight},
  2186.       {"getseedlast", 0, calc_getseedlast},
  2187.       {"getseedlastcnt", 0, calc_getseedlastcnt},
  2188.       {"getseedscorelast", 0, calc_getseedscorelast},
  2189.       {"getseedscores", 0, calc_getseedscores},
  2190.       {"hex",  0, calc_hex},
  2191.       {"html2iso", 0, calc_html2iso},
  2192.       {"htmlmath", 0, htmlmath},
  2193.       {"httpquery", 0, tohttpquery},
  2194.       {"imgrename", 0, calc_imgrename},
  2195.       {"instexst", 1, calc_instexst},
  2196.       {"instexstatic", 1, calc_instexst},
  2197.       {"item",  1, calc_itemof},
  2198.       {"itemcnt", 0,      calc_itemnum},
  2199.       {"itemcount", 0,      calc_itemnum},
  2200.       {"itemno", 0,      calc_itemnum},
  2201.       {"itemnum", 0,      calc_itemnum},
  2202.       {"items",  1, calc_itemof},
  2203.       {"items2lines", 0,      items2lines},
  2204.       {"items2words", 0,      items2words},
  2205.       {"itemstolines", 0,      items2lines},
  2206.       {"itemstowords", 0,      items2words},
  2207.       {"lengthof", 0,      calc_lengthof},
  2208.       {"leveldata", 1, calc_leveldata},
  2209.       {"levelpoints", 1, calc_leveldata},
  2210.       {"line",  1, calc_lineof},
  2211.       {"linecnt", 0,      calc_linenum},
  2212.       {"linecount", 0,      calc_linenum},
  2213.       {"lineno", 0,      calc_linenum},
  2214.       {"linenum", 0,      calc_linenum},
  2215.       {"lines",  1, calc_lineof},
  2216.       {"lines2items", 0,      lines2items},
  2217.       {"lines2list", 0,      lines2items},
  2218.       {"lines2rows", 0,      calc_lines2rows},
  2219.       {"lines2words", 0,      lines2words},
  2220.       {"linestoitems", 0,      lines2items},
  2221.       {"linestolist", 0,      lines2items},
  2222.       {"linestowords", 0,      lines2words},
  2223.       {"list2lines", 0,      items2lines},
  2224.       {"list2words", 0,      items2words},
  2225.       {"listcomplement",1, calc_listcomplement},
  2226.       {"listfile", 0, calc_listfile},
  2227.       {"listfiles", 0, calc_listfile},
  2228.       {"listintersect", 1, calc_listintersect},
  2229.       {"listintersection",1, calc_listintersect},
  2230.       {"listtolines", 0,      items2lines},
  2231.       {"listtowords", 0,      items2words},
  2232.       {"listunion", 1, calc_listunion},
  2233.       {"listuniq", 0, calc_listuniq},
  2234.       {"listunique", 0, calc_listuniq},
  2235.       {"listvar", 0, mathvarlist},
  2236.       {"lookup", 1, calc_lookup},
  2237.       {"lower",  0, calc_tolower},
  2238.       {"lowercase", 0, calc_tolower},
  2239.       {"ls",  0, calc_listfile},
  2240.       {"mailurl", 0, calc_mailurl},
  2241.       {"makelist", 1, calc_makelist},
  2242.       {"math2html", 0, htmlmath},
  2243.       {"math2mathml", 0,mathmlmath},
  2244.       {"math2tex", 0, texmath},
  2245.       {"mathmlmath", 0, mathmlmath},
  2246.       {"mathsubst", 1, calc_mathsubst},
  2247.       {"mathsubstit", 1, calc_mathsubst},
  2248.       {"mathsubstitute",1, calc_mathsubst},
  2249.       {"mexec",  0, calc_mexec},
  2250.       {"module", 0, calc_module},
  2251.       {"multiply", 1, calc_product},
  2252.       {"non_empty", 0, calc_nonempty},
  2253.       {"nonempty", 0, calc_nonempty},
  2254.       {"nospace", 0, nospace},
  2255.       {"nosubst", 1, calc_subst},
  2256.       {"nosubstit", 1, calc_subst},
  2257.       {"nosubstitute", 1, calc_subst},
  2258.       {"passcheck", 0, calc_passcheck},
  2259.       {"passcrypt", 0, calc_passcrypt},
  2260.       {"pedia",  0, pedia},
  2261.       {"perl",  0, calc_perl},
  2262.       {"position", 1, calc_pos},
  2263.       {"positionof", 1, calc_pos},
  2264.       {"positions", 1, calc_pos},
  2265.       {"prod",  1, calc_product},
  2266.       {"product", 1, calc_product},
  2267.       {"randchar", 0, calc_randchar},
  2268.       {"randdouble", 0, calc_randdouble},
  2269.       {"randfile", 0, calc_randfile},
  2270.       {"randfloat", 0, calc_randdouble},
  2271.       {"randint",  0, calc_randint},
  2272.       {"randitem", 0, calc_randitem},
  2273.       {"randline", 0, calc_randline},
  2274.       {"random", 0, calc_randdouble},
  2275.       {"randperm", 0, calc_randperm},
  2276.       {"randpermute", 0, calc_randperm},
  2277.       {"randreal", 0, calc_randdouble},
  2278.       {"randrecord", 0, calc_randfile},
  2279.       {"randrow", 0, calc_randrow},
  2280.       {"randword", 0, calc_randword},
  2281.       {"rawmath", 0, rawmath},
  2282.       {"rawmatrix", 0, rawmatrix},
  2283.       {"reaccent", 0, reaccent},
  2284.       {"record", 1, calc_recordof},
  2285.       {"recordcnt", 0, calc_recordnum},
  2286.       {"recordcount", 0, calc_recordnum},
  2287.       {"recordno", 0, calc_recordnum},
  2288.       {"recordnum", 0, calc_recordnum},
  2289.       {"records", 1, calc_recordof},
  2290.       {"recursion", 1, calc_recursion},
  2291.       {"reinput", 0, calc_reinput},
  2292.       {"rename", 0, calc_rename},
  2293.       {"replace", 1, calc_replace},
  2294.       {"rootof", 1, calc_solve},
  2295.       {"row",  1, calc_rowof},
  2296.       {"rowcnt", 0,      calc_rownum},
  2297.       {"rowcount", 0,      calc_rownum},
  2298.       {"rowno",  0,      calc_rownum},
  2299.       {"rownum", 0,      calc_rownum},
  2300.       {"rows",  1, calc_rowof},
  2301.       {"rows2lines", 0, calc_rows2lines},
  2302.       {"run",   0, calc_exec},
  2303.       {"select", 1, calc_select},
  2304.       {"sh",  0, calc_sh},
  2305.       {"shuffle", 0, calc_randperm},
  2306.       {"singlespace", 0, singlespace},
  2307.       {"slashsubst", 0, slashsubst},
  2308.       {"solve",  1, calc_solve},
  2309.       {"sort",  1, calc_sort},
  2310. /*      {"sql",  0, calc_sql}, */
  2311.       {"staticinstex", 1, calc_instexst},
  2312.       {"stinstex", 1, calc_instexst},
  2313.       {"subst",  0, calc_subst},
  2314.       {"substit", 0, calc_subst},
  2315.       {"substitute", 0, calc_subst},
  2316.       {"sum",  1, calc_sum},
  2317.       {"system", 0, calc_sh},
  2318.       {"texmath", 0, texmath},
  2319.       {"text",  1, text},
  2320.       {"tohex",  0, calc_hex},
  2321.       {"tolower", 0, calc_tolower},
  2322.       {"toupper", 0, calc_toupper},
  2323.       {"translate", 1, calc_translate},
  2324.       {"trim",  0, calc_trim},
  2325.       {"unhttp", 0, calc_unhttp},
  2326.       {"upper",  0, calc_toupper},
  2327.       {"uppercase", 0, calc_toupper},
  2328.       {"values", 1, calc_values},
  2329.       {"varlist", 0, mathvarlist},
  2330.       {"word",  1,      calc_wordof},
  2331.       {"wordcnt", 0,      calc_wordnum},
  2332.       {"wordcount", 0,      calc_wordnum},
  2333.       {"wordno", 0,      calc_wordnum},
  2334.       {"wordnum", 0,      calc_wordnum},
  2335.       {"words",  1,      calc_wordof},
  2336.       {"words2items", 0,      words2items},
  2337.       {"words2lines", 0,      words2lines},
  2338.       {"words2list", 0,      words2items},
  2339.       {"wordstoitems", 0,      words2items},
  2340.       {"wordstolines", 0,      words2lines},
  2341.       {"wordstolist", 0,      words2items}
  2342. };
  2343. int CALC_FN_NO=(sizeof(calc_routine)/sizeof(calc_routine[0]));
  2344.