Subversion Repositories wimsdev

Rev

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