Subversion Repositories wimsdev

Rev

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