Subversion Repositories wimsdev

Rev

Rev 13769 | 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. /* Interface maxima to wims */
  19.  
  20. /*************** Customization: change values hereafter ****************/
  21. #include "common.h"
  22.  
  23. /* limit of input/output file sizes */
  24. int fsizelim=131072;
  25. /* This string tells maxima to exit. */
  26. char *quitstring="\nquit();\n";
  27. char *aboutquitstring="build_info();" "\nquit();\n";
  28. /* The way to print a string in the program. */
  29. char *stringprinter="\"%s\";\n";
  30. /* This is maxima home page. To be kept up to date. */
  31. #define homepage "http://maxima.sourceforge.net/"
  32.  
  33. char *nameofcmd="maxima";
  34. int precision=20;
  35. char header[]="\n\
  36. display2d:false;\n\
  37. letrat:true;\n\
  38. keepfloat:true;\n\
  39. rombergmin:5;\n\
  40. rombergtol:1.E-6;\n\
  41. rombergit:13;\n\
  42. simpsum:true;\n\
  43. triginverses:true; logabs:true;\n\
  44. e:%e;pi:%pi;Pi:%pi;PI:%pi;I:%i;i:%i;\n\
  45. ln:log;sh:sinh;ch:cosh;th:tanh;\n\
  46. arctan:atan;arcsin:asin;arccos:acos;\n\
  47. tg:tan;arctg:atan;\n\
  48. argsh:asinh;argch:acosh;argth:atanh;\n\
  49. Argsh:asinh;Argch:acosh;Argth:atanh;\n\
  50. cotan:cot;ctg:cot;\n\
  51. log10(x):=block([],return(log(x)/log(10)));\n\
  52. log2(x):=block([],return(log(x)/log(2)));\n\
  53. lg(x):=log10(x);\n\
  54. sgn:sign;\n\
  55. nolabels:true; kill(labels);\n\
  56. ";
  57.  
  58. struct {
  59.     char *wname; char *defaultval; char *setname;
  60. } setups[]={
  61.       {"w_maxima_precision", "20", "fpprec"}
  62. };
  63.  
  64. /* names which are not allowed */
  65. char *illegal[]={
  66.       "system","describe","example",
  67.       "save","fassave","stringout","batcon",
  68.       "batcount","cursordisp",
  69.       "direc","readonly","with_stdout","pscom",
  70.       "demo","ttyintfun","bug"
  71.  
  72. };
  73.  
  74. int illegal_no=(sizeof(illegal)/sizeof(illegal[0]));
  75.  
  76. /* name parts which are not allowed */
  77. char *illpart[]={
  78.     "file", "debug", "plot", "load", "store", "batch"
  79. };
  80.  
  81. int illpart_no=(sizeof(illpart)/sizeof(illpart[0]));
  82.  
  83. /***************** Nothing should need change hereafter *****************/
  84.  
  85. #define linebyline "\n(%i"
  86. char *progname="maxima";
  87.  
  88. /* check for security violations in command string */
  89. void check_parm(char *pm)
  90. {
  91.     char *s, *pp;
  92.     int l;
  93. /* Underscore replacement */
  94.     for(pp=strchr(pm,'_'); pp!=NULL; pp=strchr(pp+1,'_')) *pp='K';
  95. /* '?' escapes to Lisp commands. */
  96.     for(pp=strchr(pm,'?'); pp!=NULL; pp=strchr(pp+1,'?')) *pp=' ';
  97.     /* if(strchr(pm,'?')!=NULL) {
  98.       fprintf(stderr,"Illegal under WIMS.\n"); exit(1);
  99.     }*/
  100.     for(s=pm;*s;s++) *s=tolower(*s);
  101.     strip_trailing_spaces2(pm); l=strlen(pm);
  102.     if(l>0 && pm[l-1]!=';') strcat(pm,";");
  103.     find_illegal(pm);
  104. }
  105.  
  106. char *find_prompt(char *p, char t)
  107. {
  108.     char *pp=p-1, *pt;
  109.     char c;
  110.     int add;
  111.     redo:
  112.     if(*p==0 || (pp>=p && *pp==0)) return NULL;
  113.     add=3;
  114.     do {
  115.       pp=strstr(pp+1,"\n(");
  116.       if(!pp) break;
  117.       c=pp[2]; add=3;
  118.       if(c=='\%') { /* backward compatibility */
  119.           add++; c=pp[3];
  120.           if(c=='i') c='C';
  121.           if(c=='o') c='D';
  122.       }
  123.     }
  124.     while(c!=t);
  125.     pt=pp;
  126.     if(pp!=NULL) {
  127.       pp+=add; while(isdigit(*pp)) pp++;
  128.       if(*pp!=')') goto redo;
  129.       pp++;
  130.     }
  131.     if(pt!=NULL && t=='D') pt=pp;
  132.     return pt;
  133. }
  134.  
  135. /* process and print maxima output */
  136. void output(char *p)
  137. {
  138.     int i,n;
  139.     char *pp, *pe, *pt;
  140.  
  141.     pp=find_prompt(p,'C');
  142.     while(pp!=NULL) {
  143.       pe=find_prompt(pp+1,'C'); pp=find_prompt(pp,'D');
  144.       if(pp==NULL) return;
  145.       if(pe!=NULL && pp>=pe) goto emptyline;
  146.       if(pe==NULL) pe=pp+strlen(pp); else *pe++=0;
  147.       if(pp>=pe) {
  148.           emptyline:
  149.           puts(""); pp=pe; continue;
  150.       }
  151.       n=strlen(pp); if(n==0) goto emptyline;
  152. /* make every output one-line */
  153.       for(i=0;i<n;i++) {
  154.           if(*(pp+i)=='\n' || *(pp+i)=='\%') *(pp+i)=' ';
  155.           else *(pp+i)=tolower(*(pp+i));
  156.       }
  157. /* strip leading and trailing spaces */
  158.       while(isspace(*pp) && pp<pe) pp++;
  159.       pt=pp+strlen(pp)-1;
  160.       while(isspace(*pt) && pt>pp) *pt--=0;
  161.       if(*pp=='[' && *pt==']' && find_matching2(pp+1,']')==pt) {
  162.           *(pt--)=0; pp++;
  163.       }
  164.       for(pt=strchr(pp,'b');pt!=NULL; pt=strchr(pt+1,'b')) {
  165.           if(pt>pp && isdigit(*(pt-1)) &&
  166.              (*(pt+1)=='-' || isdigit(*(pt+1)))) {
  167.             if(*(pt+1)=='0' && !isdigit(*(pt+2))) ovlstrcpy(pt,pt+2);
  168.             else *pt='E';
  169.           }
  170.       }
  171.       puts(pp); pp=pe;
  172.     }
  173. }
  174.  
  175. void about(void)
  176. {
  177.     char *p, *p2, *pp;
  178.     int i;
  179.  
  180. /*    printf("<a href=\"%s\">Maxima</a>",homepage); return; */
  181.     prepabout(aboutquitstring,outputfname,NULL);
  182.     if(readabout()>0) {
  183.       for(p=aboutbuf; *p; p=find_word_start(find_word_end(p))) {
  184.           if(strncasecmp(p,"Maxima",strlen("Maxima"))==0) {
  185.             p2=find_word_start(find_word_end(p));
  186.             if(strncmp(p2,"restarted",9)!=0) break;
  187.           }
  188.       }
  189.       for(p2=p;*p2 && *p2!='\n' && !isdigit(*p2);p2++);
  190.       if(isdigit(*p2)) pp=find_word_end(p2);
  191.       else for(i=0, pp=p;i<2;i++) pp=find_word_end(find_word_start(pp));
  192.       *pp=0;
  193.       if(*p!=0) printf("<a target=\"wims_external\" href=\"%s\">%s</a>",homepage,p);
  194.     }
  195. }
  196.  
  197. char *dynsetup(char *ptr, char *end)
  198. {
  199.     int i;
  200.     char *p, *pp;
  201.     if(wseed!= NULL) {
  202.       snprintf(ptr,end-ptr,"\nwimsseed:make_random_state(%u);\nset_random_state(wimsseed);\n",atoi(wseed)&(0x7FFFFFFF));
  203.       ptr+=strlen(ptr);
  204.     }
  205.     for(i=0;i<SETUP_NO;i++) {
  206.       p=getenv(setups[i].wname);
  207.       if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
  208.       if(p==NULL || *p==0) p=setups[i].defaultval;
  209.       snprintf(ptr,end-ptr,"%s:%s;\n",setups[i].setname,p);
  210.       ptr+=strlen(ptr);
  211.       if(strstr(setups[i].wname,"maxima_precision")!=NULL)
  212.         precision=atoi(p);
  213.       if(precision<0) precision=-precision;
  214.     }
  215.     return ptr;
  216. }
  217.  
  218. int main(int argc,char *argv[])
  219. {
  220.     prepare1();
  221.     run();
  222.     return 0;
  223. }
  224.  
  225.