Back to index

wims  3.65+svn20090927
octave.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 octave to wims */
00019 
00020 /* 'Permission denied' error generated by Octave: it is trying to
00021  * write to .octave.history even with the -H switch. */
00022 
00023 /*************** Customization: change values hereafter ****************/
00024 
00025        /* limit of input/output file sizes */
00026 #define fsizelim 131072
00027        /* This string tells octave to exit. */
00028 #define quitstring "\nquit\n"
00029        /* The way to print a string in the program. */
00030 #define stringprinter "\"%s\"\n"
00031        /* This is octave home page. To be kept up to date. */
00032 #define homepage "http://www.che.wisc.edu/octave/octave.html"
00033        /* String to search for answers */
00034 char ans_str[]="ans =";
00035 
00036 char *nameofcmd="octave -Hf --no-line-editing";
00037 int precision=9;
00038 char header[]="split_long_rows=0\n\
00039 page_screen_output=0\n\
00040 function y=sh(x) y=sinh(x); endfunction\n\
00041 function y=ch(x) y=cosh(x); endfunction\n\
00042 function y=th(x) y=tanh(x); endfunction\n\
00043 function y=ln(x) y=log(x); endfunction\n\
00044 function y=lg(x) y=log10(x); endfunction\n\
00045 function y=sgn(x) y=sign(x); endfunction\n\
00046 function y=tg(x) y=tan(x); endfunction\n\
00047 function y=cotan(x) y=cot(x); endfunction\n\
00048 function y=ctg(x) y=cot(x); endfunction\n\
00049 function y=arcsin(x) y=asin(x); endfunction\n\
00050 function y=arccos(x) y=acos(x); endfunction\n\
00051 function y=arctan(x) y=atan(x); endfunction\n\
00052 function y=arctg(x) y=atan(x); endfunction\n\
00053 function y=argsh(x) y=asinh(x); endfunction\n\
00054 function y=argch(x) y=acosh(x); endfunction\n\
00055 function y=argth(x) y=atanh(x); endfunction\n\
00056 function y=Argsh(x) y=asinh(x); endfunction\n\
00057 function y=Argch(x) y=acosh(x); endfunction\n\
00058 function y=Argth(x) y=atanh(x); endfunction\n\
00059 function y=rint(x) y=round(x); endfunction\n\
00060 PI=pi\n\
00061 Pi=pi\n\
00062 ";
00063 
00064 struct {
00065     char *wname;    char *defaultval;     char *setname;
00066 } setups[]={
00067        {"w_octave_precision",      "9",   "output_precision"}
00068 };
00069 
00070        /* names which are not allowed */
00071 char *illegal[]={
00072       "system","fopen","fclose","readdir","popen","mkdir","rmdir",
00073       "dir","ls","cd","chdir","more","save","load","diary",
00074       "fork","putenv","graw","eval",
00075       "scanf","exec","unlink","umask","lstat","stat","rename",
00076       "glob","tilde_expand","pclose","popen2","waitpid",
00077       
00078 };
00079 
00080        /* name parts which are not allowed */
00081 char *illpart[]={
00082     "file", "debug", "plot"
00083 };
00084 
00085 /***************** Nothing should need change hereafter *****************/
00086 
00087 #define progname "octave"
00088 #include "common.h"
00089 #include "common.c"
00090 
00091        /* check for security violations in command string */
00092 void check_parm(char *p)
00093 {
00094     char *pp, *s;
00095     
00096          /* Underscore replacement */
00097     for(pp=strchr(p,'_'); pp!=NULL; pp=strchr(pp+1,'_')) {
00098        if(pp==p || !isalnum(*(pp-1))) *pp='K';
00099     }
00100     for(s=p;*s;s++) *s=tolower(*s);
00101     find_illegal(p);
00102 }
00103 
00104        /* process and print octave output */
00105 void output(char *p)
00106 {
00107     int i,n;
00108     char *pp, *pe, *pt;
00109 
00110     pp=strchr(p,'\n');
00111     for(pp++; *pp; pp=pe) {
00112        pe=strchr(pp,'\n'); if(pe) *pe++=0; else pe=pp+strlen(pp);
00113        if(memcmp(pp,ans_str,strlen(ans_str))==0) pp+=strlen(ans_str);
00114        n=strlen(pp); if(n==0) {
00115            puts(""); continue;
00116        }
00117               /* make every output one-line */
00118        for(i=0;i<n;i++) {
00119            if(*(pp+i)=='\n' || *(pp+i)=='\%') *(pp+i)=' ';
00120        }
00121          /* strip leading and trailing spaces */
00122        while(isspace(*pp) && pp<pe) pp++;
00123        pt=pp+strlen(pp)-1;
00124        while(isspace(*pt) && pt>pp) *pt--=0;
00125        strip_zeros(pp);
00126        puts(pp);
00127     }
00128 }
00129 
00130 void about(void)
00131 {
00132     char *p;
00133 
00134     prepabout(quitstring,outputfname,NULL);
00135     if(readabout()>0) {
00136        p=strchr(aboutbuf,'\n'); if(p!=NULL) *p=0;
00137        p=strchr(aboutbuf,'('); if(p!=NULL) *p=0;
00138        strip_trailing_spaces(aboutbuf);
00139        printf("<A HREF=\"%s\">%s</A>",homepage,aboutbuf);
00140     }
00141 }
00142 
00143 char *dynsetup(char *ptr, char *end)
00144 {
00145     int i;
00146     char *p, *pp;
00147     for(i=0;i<SETUP_NO;i++) {
00148        p=getenv(setups[i].wname);
00149        if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
00150        if(p==NULL || *p==0) p=setups[i].defaultval;
00151        snprintf(ptr,end-ptr,"%s=%s\n",setups[i].setname,p);
00152        ptr+=strlen(ptr);
00153        if(strstr(setups[i].wname,"octave_precision")!=NULL)
00154          precision=atoi(p);
00155        if(precision<0) precision=-precision;
00156     }
00157     return ptr;
00158 }
00159 
00160 int main(int argc,char *argv[])
00161 {
00162     prepare1();
00163     run();
00164     return 0;    
00165 }
00166