Back to index

wims  3.65+svn20090927
pari.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 gp to wims */
00019 
00020 /* This program is now limited to pari version 2.??. */
00021 
00022 /*************** Customization: change values hereafter ****************/
00023 
00024        /* gp prompt string */
00025 #define gpprompt "\n? "
00026        /* This is the good bye string of gp, signaling end of output. */
00027 #define goodbyestring "Good bye!"
00028        /* This is PARI home page. To be kept up to date. */
00029 #define homepage "http://pari.math.u-bordeaux.fr/"
00030        /* String to tell the program to quit. */
00031 #define quitstring "\nquit\n"
00032        /* The way to print a string in the program. */
00033 #define stringprinter "print(\"%s\")\n"
00034        /* limit of input/output file sizes */
00035 int fsizelim=131072;
00036 int precision=28;    /* default */
00037 
00038 char *nameofcmd="gp";
00039 char *gprcenv="GPRC";
00040 char *gprc="../.gprc";
00041 char header[]="default(output,0)\n\
00042 alias(ln,log)\n\
00043 alias(pi,Pi)\n\
00044 alias(euler,Euler)\n\
00045 alias(PI,Pi)\n\
00046 i=I\n\
00047 e=exp(1)\n\
00048 E=exp(1)\n\
00049 { sec(x)=return(1/cos(x)) }\n\
00050 { csc(x)=return(1/sin(x)) }\n\
00051 { lg(x) =log(x)/log(10) }\n\
00052 { log2(x) =log(x)/log(10) }\n\
00053 alias(log10,lg)\n\
00054 alias(sgn,sign)\n\
00055 alias(ch,cosh)\n\
00056 alias(sh,sinh)\n\
00057 alias(th,tanh)\n\
00058 alias(arccos,acos)\n\
00059 alias(arcsin,asin)\n\
00060 alias(tg,tan)\n\
00061 alias(arctan,atan)\n\
00062 alias(arctg,atan)\n\
00063 alias(Argch,acosh)\n\
00064 alias(Argsh,asinh)\n\
00065 alias(Argth,atanh)\n\
00066 alias(argch,acosh)\n\
00067 alias(argsh,asinh)\n\
00068 alias(argth,atanh)\n\
00069 alias(cot,cotan)\n\
00070 alias(ctg,cotan)\n\
00071 alias(rint,round)\n\
00072 alias(RANDOM,random)\n\
00073 alias(SOLVE,solve)\n\
00074 alias(parirandom,random)\n\
00075 alias(parisolve,solve)\n\
00076 ";
00077 
00078 struct {
00079     char *wname;    char *defaultval;    char *gpset;
00080 } setups[]={
00081       {"w_pari_precision",  "20",  "\\p "},
00082       {"w_pari_serieslength",      "8",   "\\ps "}
00083 };
00084 
00085        /* names which are not allowed */
00086 char *illegal[]={
00087        "alias","allocatemem","default","extern",
00088       "getrand","getstack","input","psdraw",
00089       "read","system","install"
00090 };
00091 
00092        /* name parts which are not allowed */
00093 char *illpart[]={
00094     "plot", "write", "help"
00095 };
00096 
00097 /***************** Nothing should need change hereafter *****************/
00098 
00099 #define progname "pari"
00100 #include "common.h"
00101 #include "common.c"
00102 
00103 int pariray=0;
00104 
00105        /* check for security violations in command string */
00106 void check_parm(char *pm)
00107 {
00108     char *p;
00109     for(p=pm;*p!=0;p++) {
00110          /* Underscore replacement */
00111        if(*p=='_') {*p='K'; continue;}
00112          /* no escape commands. */
00113        if(*p!='\n') continue;
00114        while(*p!=0 && isspace(*p)) p++;
00115        if(*p=='\\' && *(p+1)!='\\' && *(p+1)!='v' && *(p+1)!='p') *p='.';
00116        if(*p=='\\') p++;
00117     }
00118     find_illegal(pm);
00119 }
00120 
00121        /* process and print gp output */
00122 void output(char *p)
00123 {
00124     int i,n;
00125     char *pp, *pe, *pt;
00126     
00127     if(pariray) {puts(p); return;}
00128     pp=strstr(p,gpprompt); if(pp==NULL) return;
00129     pe=strstr(pp,goodbyestring);
00130     if(pe>=pp) *pe=0;
00131     while(pp!=NULL) {
00132        pp++;
00133        pe=strstr(pp,gpprompt);
00134        if(pe>=pp) *pe=0;
00135        pp=strchr(pp,'\n');
00136        if(pp==NULL) {
00137            emptyline:
00138            puts(""); pp=pe; continue;
00139        }
00140        pp++; n=strlen(pp);
00141        if(n==0) goto emptyline;
00142               /* make every output one-line */
00143        for(i=0;i<n;i++) {
00144            if(*(pp+i)=='\n') {
00145               if(*(pp+i+1)!='%') *(pp+i)=' ';
00146               else {*(pp+i)=0; break;}
00147            }
00148        }
00149          /* strip leading and trailing spaces */
00150        while(isspace(*pp) && pp<pe) pp++;
00151        pt=pp+strlen(pp)-1;
00152        while(isspace(*pt) && pt>pp) *pt--=0;
00153          /* remove parentheses of matrix output */
00154        if(memcmp(pp,"Mat(",4)==0 && *pt==')' && find_matching(pp+4,')')==pt) {
00155            *(pt--)=0; pp+=4;
00156        }
00157        if(*pp=='[' && *pt==']' && find_matching(pp+1,']')==pt) {
00158            *(pt--)=0; pp++;
00159        }
00160        strip_zeros(pp);
00161        puts(pp); pp=pe;
00162     }
00163 }
00164 
00165 void about(void)
00166 {
00167     char *p;
00168 
00169     prepabout("\\v\nquit\n",outputfname,NULL);
00170     if(readabout()>0) {
00171        p=strchr(aboutbuf,'\n'); if(p!=NULL) *p=0;
00172        strip_trailing_spaces(aboutbuf);
00173        printf("<A HREF=\"%s\">%s</A>",homepage,aboutbuf);
00174     }
00175 }
00176 
00177 char *dynsetup(char *ptr, char *end)
00178 {
00179     int i;
00180     char *p, *pp;
00181 
00182     snprintf(ptr,end-ptr,"\nsetrand(%u)\n",seed&(0x7FFFFFFF));
00183     ptr+=strlen(ptr);
00184     for(i=0;i<SETUP_NO;i++) {
00185        p=getenv(setups[i].wname);
00186        if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
00187        if(p==NULL || *p==0) p=setups[i].defaultval;
00188        snprintf(ptr,end-ptr,"%s%s\n",setups[i].gpset,p);
00189        ptr+=strlen(ptr);
00190        if(strstr(setups[i].wname,"pari_precision")!=NULL)
00191          precision=atoi(p);
00192        if(precision<0) precision=-precision;
00193     }
00194     if(pariray) snprintf(ptr,end-ptr,"\\e0\n");
00195     else snprintf(ptr,end-ptr,"\\e1\n");
00196     ptr+=strlen(ptr);
00197     return ptr;
00198 }
00199 
00200 int main(int argc,char *argv[])
00201 {
00202     char *p;
00203 
00204     p=getenv("pari_ray");
00205     if(p!=NULL && *p!=0) {pariray=1; fsizelim=4*1024*1024;}
00206     setenv(gprcenv,gprc,1);
00207     prepare1();
00208     setenv("GPTMPDIR",tmp_dir,1);
00209     run();
00210     return 0;    
00211 }
00212