Subversion Repositories wimsdev

Rev

Rev 3901 | 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 octave to wims */
  19.  
  20. /* 'Permission denied' error generated by Octave: it is trying to
  21.  * write to .octave.history even with the -H switch. */
  22.  
  23. /*************** Customization: change values hereafter ****************/
  24.  
  25.         /* limit of input/output file sizes */
  26. #define fsizelim 131072
  27.         /* This string tells octave to exit. */
  28. #define quitstring "\nquit\n"
  29.         /* The way to print a string in the program. */
  30. #define stringprinter "\"%s\"\n"
  31.         /* This is octave home page. To be kept up to date. */
  32. #define homepage "http://www.che.wisc.edu/octave/octave.html"
  33.         /* String to search for answers */
  34. char ans_str[]="ans =";
  35.  
  36. char *nameofcmd="octave -Hf --no-line-editing";
  37. int precision=9;
  38. char header[]="split_long_rows=0\n\
  39. page_screen_output=0\n\
  40. function y=sh(x) y=sinh(x); endfunction\n\
  41. function y=ch(x) y=cosh(x); endfunction\n\
  42. function y=th(x) y=tanh(x); endfunction\n\
  43. function y=ln(x) y=log(x); endfunction\n\
  44. function y=lg(x) y=log10(x); endfunction\n\
  45. function y=sgn(x) y=sign(x); endfunction\n\
  46. function y=tg(x) y=tan(x); endfunction\n\
  47. function y=cotan(x) y=cot(x); endfunction\n\
  48. function y=ctg(x) y=cot(x); endfunction\n\
  49. function y=arcsin(x) y=asin(x); endfunction\n\
  50. function y=arccos(x) y=acos(x); endfunction\n\
  51. function y=arctan(x) y=atan(x); endfunction\n\
  52. function y=arctg(x) y=atan(x); endfunction\n\
  53. function y=argsh(x) y=asinh(x); endfunction\n\
  54. function y=argch(x) y=acosh(x); endfunction\n\
  55. function y=argth(x) y=atanh(x); endfunction\n\
  56. function y=Argsh(x) y=asinh(x); endfunction\n\
  57. function y=Argch(x) y=acosh(x); endfunction\n\
  58. function y=Argth(x) y=atanh(x); endfunction\n\
  59. function y=rint(x) y=round(x); endfunction\n\
  60. PI=pi\n\
  61. Pi=pi\n\
  62. ";
  63.  
  64. struct {
  65.     char *wname;    char *defaultval;   char *setname;
  66. } setups[]={
  67.         {"w_octave_precision",  "9",    "output_precision"}
  68. };
  69.  
  70.         /* names which are not allowed */
  71. char *illegal[]={
  72.       "system","fopen","fclose","readdir","popen","mkdir","rmdir",
  73.       "dir","ls","cd","chdir","more","save","load","diary",
  74.       "fork","putenv","graw","eval",
  75.       "scanf","exec","unlink","umask","lstat","stat","rename",
  76.       "glob","tilde_expand","pclose","popen2","waitpid",
  77.      
  78. };
  79.  
  80.         /* name parts which are not allowed */
  81. char *illpart[]={
  82.     "file", "debug", "plot"
  83. };
  84.  
  85. /***************** Nothing should need change hereafter *****************/
  86.  
  87. #define progname "octave"
  88. #include "common.h"
  89. #include "common.c"
  90.  
  91.         /* check for security violations in command string */
  92. void check_parm(char *p)
  93. {
  94.     char *pp, *s;
  95.    
  96.           /* Underscore replacement */
  97.     for(pp=strchr(p,'_'); pp!=NULL; pp=strchr(pp+1,'_')) {
  98.         if(pp==p || !isalnum(*(pp-1))) *pp='K';
  99.     }
  100.     for(s=p;*s;s++) *s=tolower(*s);
  101.     find_illegal(p);
  102. }
  103.  
  104.         /* process and print octave output */
  105. void output(char *p)
  106. {
  107.     int i,n;
  108.     char *pp, *pe, *pt;
  109.  
  110.     pp=strchr(p,'\n');
  111.     for(pp++; *pp; pp=pe) {
  112.         pe=strchr(pp,'\n'); if(pe) *pe++=0; else pe=pp+strlen(pp);
  113.         if(memcmp(pp,ans_str,strlen(ans_str))==0) pp+=strlen(ans_str);
  114.         n=strlen(pp); if(n==0) {
  115.             puts(""); continue;
  116.         }
  117.                 /* make every output one-line */
  118.         for(i=0;i<n;i++) {
  119.             if(*(pp+i)=='\n' || *(pp+i)=='\%') *(pp+i)=' ';
  120.         }
  121.           /* strip leading and trailing spaces */
  122.         while(isspace(*pp) && pp<pe) pp++;
  123.         pt=pp+strlen(pp)-1;
  124.         while(isspace(*pt) && pt>pp) *pt--=0;
  125.         strip_zeros(pp);
  126.         puts(pp);
  127.     }
  128. }
  129.  
  130. void about(void)
  131. {
  132.     char *p;
  133.  
  134.     prepabout(quitstring,outputfname,NULL);
  135.     if(readabout()>0) {
  136.         p=strchr(aboutbuf,'\n'); if(p!=NULL) *p=0;
  137.         p=strchr(aboutbuf,'('); if(p!=NULL) *p=0;
  138.         strip_trailing_spaces(aboutbuf);
  139.         printf("<A HREF=\"%s\">%s</A>",homepage,aboutbuf);
  140.     }
  141. }
  142.  
  143. char *dynsetup(char *ptr, char *end)
  144. {
  145.     int i;
  146.     char *p, *pp;
  147.     for(i=0;i<SETUP_NO;i++) {
  148.         p=getenv(setups[i].wname);
  149.         if(p!=NULL) for(pp=p;*pp;pp++) if(!isspace(*pp) && !isalnum(*pp)) p="";
  150.         if(p==NULL || *p==0) p=setups[i].defaultval;
  151.         snprintf(ptr,end-ptr,"%s=%s\n",setups[i].setname,p);
  152.         ptr+=strlen(ptr);
  153.         if(strstr(setups[i].wname,"octave_precision")!=NULL)
  154.           precision=atoi(p);
  155.         if(precision<0) precision=-precision;
  156.     }
  157.     return ptr;
  158. }
  159.  
  160. int main(int argc,char *argv[])
  161. {
  162.     prepare1();
  163.     run();
  164.     return 0;    
  165. }
  166.  
  167.