Back to index

wims  3.65+svn20090927
yacas.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 Yacas to wims */
00019 
00020 /*************** Customization: change values hereafter ****************/
00021 
00022        /* This is yacas home page. To be kept up to date. */
00023 #define homepage "http://yacas.sourceforge.net/"
00024        /* String to tell the program to quit. */
00025 #define quitstring "\nquit\n"
00026        /* The way to print a string in the program. */
00027 #define stringprinter "\"%s\"\n"
00028        /* limit of input/output file sizes */
00029 int fsizelim=131072;
00030 int precision=20;    /* default */
00031 
00032 char *inprompt="\nIn>";
00033 char *outprompt="Out>";
00034 
00035 char *nameofcmd="yacas -p";
00036 
00037 #include "yacasheader.c"
00038 
00039 struct {
00040     char *wname;    char *defaultval;    char *yacasset;
00041 } setups[]={
00042       {"w_yacas_precision", "20",  "\\precision=",},
00043       {"w_yacas_serieslength",     "8",   "\\serieslength="}
00044 };
00045 
00046        /* names which are not allowed */
00047 char *illegal[]={
00048     "SystemCall", "Use", "Vi", "GetYacasPID", "ShowPS",
00049       "MakeFunctionPlugin"
00050 };
00051 
00052        /* name parts which are not allowed */
00053 char *illpart[]={
00054     "File", "Load", "Plot"
00055 };
00056 
00057 /***************** Nothing should need change hereafter *****************/
00058 
00059 #define progname "yacas"
00060 #include "common.h"
00061 #include "common.c"
00062 
00063        /* check for security violations in command string */
00064 void check_parm(char *pm)
00065 {
00066     char *p, *pp, *p2, buf[16];
00067     int i;
00068     
00069     for(p=pm;*p!=0;p++) {
00070        if(*p!='\\') continue;
00071        if(*(p+1)=='\n') *p=*(p+1)=' ';
00072        if(*(p+1)==0) *p=' ';
00073     }
00074     for(p=pm; *p!=0; p++) {
00075        if(!isalpha(*p)) continue;
00076        for(p2=p+1; isalpha(*p2); p2++);
00077        if(p2-p>10) {p=p2-1; continue;}
00078        memmove(buf,p,p2-p); buf[p2-p]=0; pp=p; p=p2-1;
00079        for(p2=buf; islower(*p2); p2++);
00080        if(*p2) continue;
00081        i=search_list(firstupper,firstupperno,sizeof(firstupper[0]),buf);
00082        if(i>=0) *pp=toupper(*pp);
00083     }
00084     find_illegal(pm);
00085 }
00086 
00087 
00088        /* process and print yacas output */
00089 void output(char *p)
00090 {
00091     int i,n;
00092     char *pp, *pe, *p1, *pt;
00093     char outbuf[MAX_LINELEN+1];
00094     
00095     pp=strstr(p,inprompt); if(pp==NULL) return;
00096     while(pp!=NULL && *pp!=0) {
00097        pp+=strlen(inprompt); p1=find_word_start(pp);
00098        pe=strstr(pp,outprompt);
00099        pt=strstr(pp,inprompt);
00100        if(pe==NULL || (pt!=NULL && pt<pe)) { /* error */
00101            if(pt==NULL) pp=pp+strlen(pp); else pp=pt;
00102            n=pp-p1; if(n>MAX_LINELEN) n=MAX_LINELEN; if(n<0) n=0;
00103            memmove(outbuf,p1,n); outbuf[n]=0;
00104            fprintf(stderr,"%s\n",outbuf);
00105            puts(""); continue;
00106        }
00107        else {
00108            *pe=0; pe=find_word_start(pe+strlen(outprompt));
00109        }
00110        pp=strstr(pe,inprompt);
00111        if(pp==NULL) pp=pe+strlen(pe);
00112        if(*p1==0) {
00113            if(pp>=pe+sizeof(outbuf)) break;
00114            n=pp-pe; if(n>MAX_LINELEN) n=MAX_LINELEN; if(n<0) n=0;
00115            memmove(outbuf,pe,n); outbuf[n]=0;
00116        }
00117        else {
00118            snprintf(outbuf,sizeof(outbuf),"%s",p1);
00119            n=strlen(outbuf);
00120        }
00121               /* make every output one-line */
00122        for(i=0;i<n;i++) {
00123            if(outbuf[i]=='\n') outbuf[i]=' ';
00124        }
00125          /* strip leading and trailing spaces */
00126        for(i=n-1;i>=0 && isspace(outbuf[i]); i--) outbuf[i]=0;
00127        if(outbuf[i]==';') outbuf[i]=0;
00128        for(pe=outbuf; *pe; pe++) if(isupper(*pe)) *pe=tolower(*pe);
00129        for(pe=outbuf; isspace(*pe); pe++);
00130        /* strip_zeros(pe); */
00131        puts(pe);
00132     }
00133 }
00134 
00135 void about(void)
00136 {
00137     cmdparm="-v"; prepabout("",outputfname,NULL);
00138     if(readabout()>0)
00139       printf("<A HREF=\"%s\">Yacas %s</A>",homepage,aboutbuf);
00140 }
00141 
00142 char *dynsetup(char *ptr, char *end)
00143 {
00144     int i; char *p, *pp;
00145     
00146     for(i=0;i<SETUP_NO;i++) {
00147        p=getenv(setups[i].wname);
00148        if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
00149        if(p==NULL || *p==0) p=setups[i].defaultval;
00150        if(strstr(setups[i].wname,"yacas_precision")!=NULL)
00151          precision=atoi(p);
00152        if(precision<0) precision=-precision;
00153     }
00154     return ptr;
00155 }
00156 
00157 int main(int argc,char *argv[])
00158 {
00159     prepare1();
00160     run();
00161     return 0;    
00162 }
00163