Subversion Repositories wimsdev

Rev

Rev 8529 | 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. /* Interface gp to wims */
  19.  
  20. /* This program is now limited to pari version 2.??. */
  21.  
  22. /*************** Customization: change values hereafter ****************/
  23. #include "common.h"
  24.  
  25. /* gp prompt string */
  26. #define gpprompt "\n? "
  27. /* This is the good bye string of gp, signaling end of output. */
  28. #define goodbyestring "Good bye!"
  29. /* This is PARI home page. To be kept up to date. */
  30. #define homepage "http://pari.math.u-bordeaux.fr/"
  31. /* String to tell the program to quit. */
  32. char *quitstring="\nquit\n";
  33. /* The way to print a string in the program. */
  34. char *stringprinter="print(\"%s\")\n";
  35. /* limit of input/output file sizes */
  36. int fsizelim=131072;
  37. int precision=28; /* default */
  38.  
  39. char *nameofcmd="gp";
  40. char *gprcenv="GPRC";
  41. char *gprc="../.gprc";
  42. char header[]="default(output,0)\n\
  43. default(secure,1)\n\
  44. alias(ln,log)\n\
  45. alias(pi,Pi)\n\
  46. alias(euler,Euler)\n\
  47. alias(PI,Pi)\n\
  48. i=I\n\
  49. e=exp(1)\n\
  50. E=exp(1)\n\
  51. sec(x)=1/cos(x);\n\
  52. csc(x)=1/sin(x);\n\
  53. lg(x)=log(x)/log(10);\n\
  54. log2(x)=log(x)/log(10);\n\
  55. alias(log10,lg)\n\
  56. alias(sgn,sign)\n\
  57. alias(ch,cosh)\n\
  58. alias(sh,sinh)\n\
  59. alias(th,tanh)\n\
  60. alias(arccos,acos)\n\
  61. alias(arcsin,asin)\n\
  62. alias(tg,tan)\n\
  63. alias(arctan,atan)\n\
  64. alias(arctg,atan)\n\
  65. alias(Argch,acosh)\n\
  66. alias(Argsh,asinh)\n\
  67. alias(Argth,atanh)\n\
  68. alias(argch,acosh)\n\
  69. alias(argsh,asinh)\n\
  70. alias(argth,atanh)\n\
  71. alias(cot,cotan)\n\
  72. alias(ctg,cotan)\n\
  73. alias(rint,round)\n\
  74. alias(RANDOM,random)\n\
  75. alias(SOLVE,solve)\n\
  76. alias(parirandom,random)\n\
  77. alias(parisolve,solve)\n\
  78. ";
  79.  
  80. struct {
  81.     char *wname; char *defaultval; char *gpset;
  82. } setups[]={
  83.       {"w_pari_precision", "20", "\\p "},
  84.       {"w_pari_serieslength", "8", "\\ps "}
  85. };
  86.  
  87. /* names which are not allowed */
  88. char *illegal[]={
  89. };
  90.  
  91. int illegal_no=(sizeof(illegal)/sizeof(illegal[0]));
  92.  
  93. /* name parts which are not allowed */
  94. char *illpart[]={
  95.     "plot", "write", "help"
  96. };
  97.  
  98. int illpart_no=(sizeof(illpart)/sizeof(illpart[0]));
  99.  
  100. /***************** Nothing should need change hereafter *****************/
  101.  
  102. char *progname="pari";
  103.  
  104. int pariray=0;
  105.  
  106. /* check for security violations in command string */
  107. void check_parm(char *pm)
  108. {
  109.     char *p;
  110.     for(p=pm;*p!=0;p++) {
  111. /* Underscore replacement */
  112.       if(*p=='_') {*p='K'; continue;}
  113. /* no escape commands. */
  114.       if(*p!='\n') continue;
  115.       while(*p!=0 && isspace(*p)) p++;
  116.       if(*p=='\\' && *(p+1)!='\\' && *(p+1)!='v' && *(p+1)!='p') *p='.';
  117.       if(*p=='\\') p++;
  118.     }
  119.     find_illegal(pm);
  120. }
  121.  
  122. /* process and print gp output */
  123. void output(char *p)
  124. {
  125.     int i,n;
  126.     char *pp, *pe, *pt;
  127.     if(pariray) {puts(p); return;}
  128.     pp=strstr(p,gpprompt); if(pp==NULL) return;
  129.     pe=strstr(pp,goodbyestring);
  130.     if(pe>=pp) *pe=0;
  131.     while(pp!=NULL) {
  132.       pp++;
  133.       pe=strstr(pp,gpprompt);
  134.       if(pe>=pp) *pe=0;
  135.       pp=strchr(pp,'\n');
  136.       if(pp==NULL) {
  137.           emptyline:
  138.           puts(""); pp=pe; continue;
  139.       }
  140.       pp++; n=strlen(pp);
  141.       if(n==0) goto emptyline;
  142. /* make every output one-line */
  143.       for(i=0;i<n;i++) {
  144.           if(*(pp+i)=='\n') {
  145.             if(*(pp+i+1)!='%') *(pp+i)=' ';
  146.             else {*(pp+i)=0; break;}
  147.           }
  148.       }
  149. /* strip leading and trailing spaces */
  150.       while(isspace(*pp) && pp<pe) pp++;
  151.       pt=pp+strlen(pp)-1;
  152.       while(isspace(*pt) && pt>pp) *pt--=0;
  153. /* remove parentheses of matrix output */
  154.       if(memcmp(pp,"Mat(",4)==0 && *pt==')' && find_matching2(pp+4,')')==pt) {
  155.           *(pt--)=0; pp+=4;
  156.       }
  157.       if(memcmp(pp,"Vecsmall(",9)==0 && *pt==')' && find_matching2(pp+9,')')==pt) {
  158.           *(pt--)=0; pp+=9;
  159.       }
  160.       if(*pp=='[' && *pt==']' && find_matching2(pp+1,']')==pt) {
  161.           *(pt--)=0; pp++;
  162.       }
  163.       strip_zeros(pp);
  164.       puts(pp); pp=pe;
  165.     }
  166. }
  167.  
  168. void about(void)
  169. {
  170.     char *p;
  171.  
  172.     prepabout("\\v\nquit\n",outputfname,NULL);
  173.     if(readabout()>0) {
  174.       p=strchr(aboutbuf,'\n'); if(p!=NULL) *p=0;
  175.       strip_trailing_spaces2(aboutbuf);
  176.       printf("<a target=\"wims_external\" href=\"%s\">%s</a>",homepage,aboutbuf);
  177.     }
  178. }
  179.  
  180. char *dynsetup(char *ptr, char *end)
  181. {
  182.     int i;
  183.     char *p, *pp;
  184.     if(wseed!= NULL) snprintf(ptr,end-ptr,"\nsetrand(%u)\n",atoi(wseed)&(0x7FFFFFFF));
  185.     else snprintf(ptr,end-ptr,"\nsetrand(%u);\n",seed&(0x7FFFFFFF));
  186.     ptr+=strlen(ptr);
  187.     for(i=0;i<SETUP_NO;i++) {
  188.       p=getenv(setups[i].wname);
  189.       if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
  190.       if(p==NULL || *p==0) p=setups[i].defaultval;
  191.       snprintf(ptr,end-ptr,"%s%s\n",setups[i].gpset,p);
  192.       ptr+=strlen(ptr);
  193.       if(strstr(setups[i].wname,"pari_precision")!=NULL)
  194.         precision=atoi(p);
  195.       if(precision<0) precision=-precision;
  196.     }
  197.     if(pariray) snprintf(ptr,end-ptr,"\\e0\n");
  198.     else snprintf(ptr,end-ptr,"\\e1\n");
  199.     ptr+=strlen(ptr);
  200.     return ptr;
  201. }
  202.  
  203. int main(int argc,char *argv[])
  204. {
  205.     char *p;
  206.  
  207.     p=getenv("pari_ray");
  208.     if(p!=NULL && *p!=0) {pariray=1; fsizelim=4*1024*1024;}
  209.     setenv(gprcenv,gprc,1);
  210.     prepare1();
  211.     setenv("GPTMPDIR",tmp_dir,1);
  212.     run();
  213.     return 0;
  214. }
  215.  
  216.