Back to index

wims  3.65+svn20090927
maxima.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* Interface maxima to wims */
00019 
00020 /*************** Customization: change values hereafter ****************/
00021 
00022        /* limit of input/output file sizes */
00023 #define fsizelim 131072
00024        /* This string tells maxima to exit. */
00025 #define quitstring "\nquit();\n"
00026        /* The way to print a string in the program. */
00027 #define stringprinter "\"%s\";\n"
00028        /* This is maxima home page. To be kept up to date. */
00029 #define homepage "http://maxima.sourceforge.net/"
00030 
00031 char *nameofcmd="maxima";
00032 int precision=20;
00033 char header[]="\n\
00034 display2d:false;\n\
00035 letrat:true;\n\
00036 keepfloat:true;\n\
00037 rombergmin:5;\n\
00038 rombergtol:1.E-6;\n\
00039 rombergit:13;\n\
00040 simpsum:true;\n\
00041 triginverses:true; logabs:true;\n\
00042 e:%e;pi:%PI;Pi:%PI;PI:%PI;I:%I;i:%I;\n\
00043 ln:log;sh:sinh;ch:cosh;th:tanh;\n\
00044 arctan:atan;arcsin:asin;arccos:acos;\n\
00045 tg:tan;arctg:atan;\n\
00046 argsh:asinh;argch:acosh;argth:atanh;\n\
00047 Argsh:asinh;Argch:acosh;Argth:atanh;\n\
00048 cotan:cot;ctg:cot;\n\
00049 log10(x):=block([],return(log(x)/log(10.0)));\n\
00050 log2(x):=block([],return(log(x)/log(2.0)));\n\
00051 lg(x):=log10(x);\n\
00052 sgn:sign;\n\
00053 nolabels:true; kill(labels);\n\
00054 ";
00055 
00056 struct {
00057     char *wname;    char *defaultval;     char *setname;
00058 } setups[]={
00059        {"w_maxima_precision",      "20",  "fpprec"}
00060 };
00061 
00062        /* names which are not allowed */
00063 char *illegal[]={
00064       "system","describe","example",
00065       "save","fassave","stringout","batcon",
00066       "batcount","cursordisp",
00067       "direc","readonly","with_stdout","pscom",
00068       "demo","ttyintfun","bug"
00069       
00070 };
00071 
00072        /* name parts which are not allowed */
00073 char *illpart[]={
00074     "file", "debug", "plot", "load", "store", "batch"
00075 };
00076 
00077 /***************** Nothing should need change hereafter *****************/
00078 
00079 #define linebyline "\n(%i"
00080 #define progname "maxima"
00081 #include "common.h"
00082 #include "common.c"
00083 
00084        /* check for security violations in command string */
00085 void check_parm(char *pm)
00086 {
00087     char *s, *pp;
00088     int l;
00089          /* Underscore replacement */
00090     for(pp=strchr(pm,'_'); pp!=NULL; pp=strchr(pp+1,'_')) *pp='K';
00091        /* '?' escapes to Lisp commands. */
00092     if(strchr(pm,'?')!=NULL) {
00093          fprintf(stderr,"Illegal under WIMS.\n"); exit(1);
00094     }
00095     for(s=pm;*s;s++) *s=tolower(*s);
00096     strip_trailing_spaces(pm); l=strlen(pm);
00097     if(l>0 && pm[l-1]!=';') strcat(pm,";");
00098     find_illegal(pm);
00099 }
00100 
00101 char *find_prompt(char *p, char t)
00102 {
00103     char *pp=p-1, *pt;
00104     char c;
00105     int add;
00106     redo:
00107     if(*p==0 || (pp>=p && *pp==0)) return NULL;
00108     add=3;
00109     do {
00110        pp=strstr(pp+1,"\n("); 
00111        if(!pp) break;
00112        c=pp[2]; add=3;
00113        if(c=='\%') { /* backward compatibility */
00114            add++; c=pp[3];
00115            if(c=='i') c='C';
00116            if(c=='o') c='D';
00117        }
00118     }
00119     while(c!=t);
00120     pt=pp;
00121     if(pp!=NULL) {
00122        pp+=add; while(isdigit(*pp)) pp++;
00123        if(*pp!=')') goto redo;
00124        pp++;
00125     }
00126     if(pt!=NULL && t=='D') pt=pp;
00127     return pt;
00128 }
00129 
00130        /* process and print maxima output */
00131 void output(char *p)
00132 {
00133     int i,n;
00134     char *pp, *pe, *pt;
00135 
00136     pp=find_prompt(p,'C');
00137     while(pp!=NULL) {
00138        pe=find_prompt(pp+1,'C'); pp=find_prompt(pp,'D');
00139        if(pp==NULL) return;
00140        if(pe!=NULL && pp>=pe) goto emptyline;
00141        if(pe==NULL) pe=pp+strlen(pp); else *pe++=0;
00142        if(pp>=pe) {
00143            emptyline:
00144            puts(""); pp=pe; continue;
00145        }
00146        n=strlen(pp); if(n==0) goto emptyline;
00147               /* make every output one-line */
00148        for(i=0;i<n;i++) {
00149            if(*(pp+i)=='\n' || *(pp+i)=='\%') *(pp+i)=' ';
00150            else *(pp+i)=tolower(*(pp+i));
00151        }
00152          /* strip leading and trailing spaces */
00153        while(isspace(*pp) && pp<pe) pp++;
00154        pt=pp+strlen(pp)-1;
00155        while(isspace(*pt) && pt>pp) *pt--=0;
00156        if(*pp=='[' && *pt==']' && find_matching(pp+1,']')==pt) {
00157            *(pt--)=0; pp++;
00158        }
00159        for(pt=strchr(pp,'b');pt!=NULL; pt=strchr(pt+1,'b')) {
00160            if(pt>pp && isdigit(*(pt-1)) && 
00161               (*(pt+1)=='-' || isdigit(*(pt+1)))) {
00162               if(*(pt+1)=='0' && !isdigit(*(pt+2))) strcpy(pt,pt+2);
00163               else *pt='E';
00164            }
00165        }
00166        puts(pp); pp=pe;
00167     }
00168 }
00169 
00170 void about(void)
00171 {
00172     char *p, *p2, *pp;
00173     int i;
00174 
00175 /*    printf("<A HREF=\"%s\">Maxima</A>",homepage); return; */
00176     prepabout("build_info();\n" quitstring,outputfname,NULL);
00177     if(readabout()>0) {
00178        for(p=aboutbuf; *p; p=find_word_start(find_word_end(p))) {
00179            if(strncasecmp(p,"Maxima",strlen("Maxima"))==0) {
00180               p2=find_word_start(find_word_end(p));
00181               if(strncmp(p2,"restarted",9)!=0) break;
00182            }
00183        }
00184        for(p2=p;*p2 && *p2!='\n' && !isdigit(*p2);p2++);
00185        if(isdigit(*p2)) pp=find_word_end(p2);
00186        else for(i=0, pp=p;i<2;i++) pp=find_word_end(find_word_start(pp));
00187        *pp=0;
00188        if(*p!=0) printf("<A HREF=\"%s\">%s</A>",homepage,p);
00189     }
00190 }
00191 
00192 char *dynsetup(char *ptr, char *end)
00193 {
00194     int i;
00195     char *p, *pp;
00196     for(i=0;i<SETUP_NO;i++) {
00197        p=getenv(setups[i].wname);
00198        if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
00199        if(p==NULL || *p==0) p=setups[i].defaultval;
00200        snprintf(ptr,end-ptr,"%s:%s;\n",setups[i].setname,p);
00201        ptr+=strlen(ptr);
00202        if(strstr(setups[i].wname,"maxima_precision")!=NULL)
00203          precision=atoi(p);
00204        if(precision<0) precision=-precision;
00205     }
00206     return ptr;
00207 }
00208 
00209 int main(int argc,char *argv[])
00210 {
00211     prepare1();
00212     run();
00213     return 0;    
00214 }
00215