Subversion Repositories wimsdev

Rev

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