home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Plotting / aa_Intel_Only / Gnuplot / GnuplotSource / plot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  12.3 KB  |  478 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: plot.c,v 1.4 1993/05/18 03:55:50 davis Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - plot.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  * Send your comments or suggestions to 
  35.  *  info-gnuplot@dartmouth.edu.
  36.  * This is a mailing list; to join it send a note to 
  37.  *  info-gnuplot-request@dartmouth.edu.  
  38.  * Send bug reports to
  39.  *  bug-gnuplot@dartmouth.edu.
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <setjmp.h>
  44. #include <signal.h>
  45. #ifdef XPG3_LOCALE
  46. #include <locale.h>
  47. #endif
  48. #include "plot.h"
  49. #include "setshow.h"
  50. #if defined(MSDOS) || defined(DOS386)
  51. #include <io.h>
  52. #endif
  53. #ifdef vms
  54. #include <unixio.h>
  55. #include <smgdef.h>
  56. extern int vms_vkid;
  57. extern smg$create_virtual_keyboard();
  58. unsigned int status[2] = {1, 0};
  59. #endif
  60. #ifdef AMIGA_SC_6_1
  61. #include <proto/dos.h>
  62. #endif
  63.  
  64. #ifdef _Windows
  65. #include <windows.h>
  66. #ifndef SIGINT
  67. #define SIGINT 2    /* for MSC */
  68. #endif
  69. #else
  70. # ifdef __TURBOC__
  71. # include <graphics.h>
  72. # endif
  73. #endif
  74.  
  75. #ifndef AMIGA_SC_6_1
  76. extern char *getenv(),*strcat(),*strcpy(),*strncpy();
  77. #endif /* !AMIGA_SC_6_1 */
  78.  
  79. extern char input_line[];
  80. extern int c_token;
  81. extern FILE *outfile;
  82. extern int term;
  83.  
  84. TBOOLEAN interactive = TRUE;    /* FALSE if stdin not a terminal */
  85. TBOOLEAN noinputfiles = TRUE;    /* FALSE if there are script files */
  86. char *infile_name = NULL;    /* name of command file; NULL if terminal */
  87.  
  88. #ifndef STDOUT
  89. #define STDOUT 1
  90. #endif
  91.  
  92. #ifdef _Windows
  93. jmp_buf far env;
  94. #else
  95. jmp_buf env;
  96. #endif
  97.  
  98. struct value *Ginteger(),*Gcomplex();
  99.  
  100.  
  101. extern f_push(),f_pushc(),f_pushd1(),f_pushd2(),f_pushd(),f_call(),f_calln(),
  102.     f_lnot(),f_bnot(),f_uminus(),f_lor(),f_land(),f_bor(),f_xor(),
  103.     f_band(),f_eq(),f_ne(),f_gt(),f_lt(),
  104.     f_ge(),f_le(),f_plus(),f_minus(),f_mult(),f_div(),f_mod(),f_power(),
  105.     f_factorial(),f_bool(),f_jump(),f_jumpz(),f_jumpnz(),f_jtern();
  106.  
  107. extern f_real(),f_imag(),f_arg(),f_conjg(),f_sin(),f_cos(),f_tan(),f_asin(),
  108.     f_acos(),f_atan(),f_sinh(),f_cosh(),f_tanh(),f_int(),f_abs(),f_sgn(),
  109.     f_sqrt(),f_exp(),f_log10(),f_log(),f_besj0(),f_besj1(),f_besy0(),f_besy1(),
  110.     f_erf(), f_erfc(), f_gamma(), f_lgamma(), f_ibeta(), f_igamma(), f_rand(),
  111.     f_floor(),f_ceil(),
  112.     f_normal(), f_inverse_erf(), f_inverse_normal();   /* XXX - JG */
  113.  
  114.  
  115. struct ft_entry ft[] = {    /* built-in function table */
  116.  
  117. /* internal functions: */
  118.     {"push", f_push},    {"pushc", f_pushc},
  119.     {"pushd1", f_pushd1},    {"pushd2", f_pushd2},    {"pushd", f_pushd},
  120.     {"call", f_call},    {"calln", f_calln},    {"lnot", f_lnot},
  121.     {"bnot", f_bnot},    {"uminus", f_uminus},    {"lor", f_lor},
  122.     {"land", f_land},    {"bor", f_bor},        {"xor", f_xor},
  123.     {"band", f_band},    {"eq", f_eq},        {"ne", f_ne},
  124.     {"gt", f_gt},        {"lt", f_lt},        {"ge", f_ge},
  125.     {"le", f_le},        {"plus", f_plus},    {"minus", f_minus},
  126.     {"mult", f_mult},    {"div", f_div},        {"mod", f_mod},
  127.     {"power", f_power}, {"factorial", f_factorial},
  128.     {"bool", f_bool},    {"jump", f_jump},    {"jumpz", f_jumpz},
  129.     {"jumpnz",f_jumpnz},{"jtern", f_jtern},
  130.  
  131. /* standard functions: */
  132.     {"real", f_real},    {"imag", f_imag},    {"arg", f_arg},
  133.     {"conjg", f_conjg}, {"sin", f_sin},        {"cos", f_cos},
  134.     {"tan", f_tan},        {"asin", f_asin},    {"acos", f_acos},
  135.     {"atan", f_atan},    {"sinh", f_sinh},    {"cosh", f_cosh},
  136.     {"tanh", f_tanh},    {"int", f_int},        {"abs", f_abs},
  137.     {"sgn", f_sgn},        {"sqrt", f_sqrt},    {"exp", f_exp},
  138.     {"log10", f_log10},    {"log", f_log},        {"besj0", f_besj0},
  139.     {"besj1", f_besj1},    {"besy0", f_besy0},    {"besy1", f_besy1},
  140.         {"erf", f_erf},         {"erfc", f_erfc},       {"gamma", f_gamma},     {"lgamma", f_lgamma},
  141.         {"ibeta", f_ibeta},     {"igamma", f_igamma},   {"rand", f_rand},
  142.         {"floor", f_floor},     {"ceil", f_ceil},
  143.  
  144.     {"norm",        f_normal},              /* XXX-JG */
  145.     {"inverf",      f_inverse_erf},         /* XXX-JG */
  146.     {"invnorm",     f_inverse_normal},      /* XXX-JG */
  147.  
  148.     {NULL, NULL}
  149. };
  150.  
  151. static struct udvt_entry udv_pi = {NULL, "pi",FALSE};
  152.                                     /* first in linked list */
  153. struct udvt_entry *first_udv = &udv_pi;
  154. struct udft_entry *first_udf = NULL;
  155.  
  156.  
  157.  
  158. #ifdef vms
  159.  
  160. #define HOME "sys$login:"
  161.  
  162. #else /* vms */
  163. #if defined(MSDOS) ||  defined(AMIGA_AC_5) || defined(AMIGA_SC_6_1) || defined(ATARI) || defined(OS2) || defined(_Windows) || defined(DOS386)
  164.  
  165. #define HOME "GNUPLOT"
  166.  
  167. #else /* MSDOS || AMIGA || ATARI || OS2 || _Windows || defined(DOS386)*/
  168.  
  169. #define HOME "HOME"
  170.  
  171. #endif /* MSDOS || AMIGA || ATARI || OS2 || _Windows || defined(DOS386)*/
  172. #endif /* vms */
  173.  
  174. #if defined(unix) || defined(AMIGA_AC_5) || defined(AMIGA_SC_6_1)
  175. #define PLOTRC ".gnuplot"
  176. #else /* AMIGA || unix */
  177. #define PLOTRC "gnuplot.ini"
  178. #endif /* AMIGA || unix */
  179.  
  180. #if defined (__TURBOC__) || defined (__PUREC__)
  181. void tc_interrupt()
  182. #else
  183. #ifdef __ZTC__
  184. void ztc_interrupt()
  185. #else
  186. #if defined( _CRAY ) || defined( sgi )
  187. void inter(an_int)
  188. int an_int;
  189. #else
  190. #if defined( NEXT ) || defined( OS2 ) || defined( VMS )
  191. void inter(int an_int)
  192. #else
  193. #ifdef sgi
  194. void inter(int sig, int code, struct sigcontext *sc)
  195. #else
  196. inter()
  197. #endif
  198. #endif
  199. #endif
  200. #endif
  201. #endif
  202. {
  203. #if defined (MSDOS) || defined(_Windows) || (defined (ATARI) && defined(__PUREC__)) || defined(DOS386)
  204. #if defined (__TURBOC__) || defined (__PUREC__)
  205. #ifndef DOSX286
  206.     (void) signal(SIGINT, tc_interrupt);
  207. #endif
  208. #else
  209. #ifdef __ZTC__
  210.    (void) signal(SIGINT, ztc_interrupt);
  211. #else
  212. #ifdef __EMX__
  213.     (void) signal(SIGINT, (void *)inter);
  214. #else
  215. #ifdef DJGPP
  216.     (void) signal(SIGINT, (SignalHandler)inter);
  217. #else
  218. #if defined __MSC__
  219.     (void) signal(SIGINT, inter);
  220. #endif    /* __MSC__ */
  221.  
  222. #endif    /* DJGPP */
  223. #endif  /* __EMX__ */
  224. #endif    /* ZTC */
  225. #endif  /* __TURBOC__ */
  226.  
  227. #else  /* MSDOS */
  228. #ifdef OS2
  229.     (void) signal(an_int, SIG_ACK);
  230. #else
  231.     (void) signal(SIGINT, inter);
  232. #endif  /* OS2 */
  233. #endif  /* MSDOS */
  234. #ifndef DOSX286
  235.     (void) signal(SIGFPE, SIG_DFL);    /* turn off FPE trapping */
  236. #endif
  237.     if (term && term_init)
  238.         (*term_tbl[term].text)();    /* hopefully reset text mode */
  239.     (void) fflush(outfile);
  240.     (void) putc('\n',stderr);
  241.     longjmp(env, TRUE);        /* return to prompt */
  242. }
  243.  
  244.  
  245. #if defined(_Windows) || defined(NEXTFE)
  246. gnu_main(argc, argv)
  247. #else
  248. main(argc, argv)
  249. #endif
  250.     int argc;
  251.     char **argv;
  252. {
  253. #ifdef XPG3_LOCALE
  254.     (void) setlocale(LC_CTYPE, "");
  255. #endif
  256. /* Register the Borland Graphics Interface drivers. If they have been */
  257. /* included by the linker.                                            */
  258.  
  259. #ifndef DOSX286
  260. #ifndef _Windows
  261. #if defined (__TURBOC__) && defined (MSDOS)
  262. registerfarbgidriver(EGAVGA_driver_far);
  263. registerfarbgidriver(CGA_driver_far);
  264. registerfarbgidriver(Herc_driver_far);
  265. registerfarbgidriver(ATT_driver_far);
  266. # endif
  267. #endif
  268. #endif
  269. #ifdef X11
  270.      { int n = X11_args(argc, argv); argv += n; argc -= n; }
  271. #endif 
  272.  
  273. #ifdef apollo
  274.     apollo_pfm_catch();
  275. #endif
  276.  
  277.     setbuf(stderr,(char *)NULL);
  278. #ifdef UNIX
  279.     setlinebuf(stdout);
  280. #endif
  281.     outfile = stdout;
  282.     (void) Gcomplex(&udv_pi.udv_value, Pi, 0.0);
  283.  
  284.      interactive = FALSE;
  285.      init_terminal();        /* can set term type if it likes */
  286.  
  287. #ifdef AMIGA_SC_6_1
  288.      if (IsInteractive(Input()) == DOSTRUE) interactive = TRUE;
  289.      else interactive = FALSE;
  290. #else
  291. #if defined(__MSC__) && defined(_Windows)
  292.      interactive = TRUE;
  293. #else
  294.      interactive = isatty(fileno(stdin));
  295. #endif
  296. #endif
  297.      if (argc > 1)
  298.       interactive = noinputfiles = FALSE;
  299.      else
  300.       noinputfiles = TRUE;
  301.  
  302.      if (interactive)
  303.       show_version();
  304. #ifdef vms   /* initialise screen management routines for command recall */
  305.           if (status[1] = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL)
  306.                done(status[1]);
  307. #endif
  308.  
  309.     if (!setjmp(env)) {
  310.         /* first time */
  311.         interrupt_setup();
  312.         load_rcfile();
  313.  
  314.         if (interactive && term != 0)    /* not unknown */
  315.          fprintf(stderr, "\nTerminal type set to '%s'\n", 
  316.                 term_tbl[term].name);
  317.     } else {    
  318.         /* come back here from int_error() */
  319.         load_file_error();    /* if we were in load_file(), cleanup */
  320. #ifdef _Windows
  321.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  322. #endif
  323. #ifdef vms
  324.         /* after catching interrupt */
  325.         /* VAX stuffs up stdout on SIGINT while writing to stdout,
  326.           so reopen stdout. */
  327.         if (outfile == stdout) {
  328.            if ( (stdout = freopen("SYS$OUTPUT","w",stdout))  == NULL) {
  329.               /* couldn't reopen it so try opening it instead */
  330.               if ( (stdout = fopen("SYS$OUTPUT","w"))  == NULL) {
  331.                  /* don't use int_error here - causes infinite loop! */
  332.                  fprintf(stderr,"Error opening SYS$OUTPUT as stdout\n");
  333.               }
  334.            }
  335.            outfile = stdout;
  336.         }
  337. #endif                    /* VMS */
  338.         if (!interactive && !noinputfiles) {
  339.             if (term && term_init)
  340.                 (*term_tbl[term].reset)();
  341. #ifdef vms
  342.             vms_reset();
  343. #endif
  344.             return(IO_ERROR);    /* exit on non-interactive error */
  345.          }
  346.     }
  347.  
  348.      if (argc > 1) {
  349.         /* load filenames given as arguments */
  350.         while (--argc > 0) {
  351.            ++argv;
  352.            c_token = NO_CARET; /* in case of file not found */
  353.            load_file(fopen(*argv,"r"), *argv);    
  354.         }
  355.     } else {
  356.         /* take commands from stdin */
  357.         while(!com_line());
  358.     }
  359.  
  360.     if (term && term_init)
  361.         (*term_tbl[term].reset)();
  362. #ifdef vms
  363.     vms_reset();
  364. #endif
  365.     return(IO_SUCCESS);
  366. }
  367.  
  368. #if defined(ATARI) && defined(__PUREC__)
  369. #include <math.h>
  370. int purec_matherr(struct exception *e)
  371. {    char *c;
  372.     switch (e->type) {
  373.         case DOMAIN:    c = "domain error"; break;
  374.         case SING  :    c = "argument singularity"; break;
  375.         case OVERFLOW:  c = "overflow range"; break;
  376.         case UNDERFLOW: c = "underflow range"; break;
  377.         default:        c = "(unknown error"; break;
  378.     }
  379.     fprintf(stderr, "math exception : %s\n", c);
  380.     fprintf(stderr, "    name : %s\n", e->name);
  381.     fprintf(stderr, "    arg 1: %e\n", e->arg1);
  382.     fprintf(stderr, "    arg 2: %e\n", e->arg2);
  383.     fprintf(stderr, "    ret  : %e\n", e->retval);
  384.     return 1;
  385. }
  386. #endif
  387.  
  388. /* Set up to catch interrupts */
  389. interrupt_setup()
  390. {
  391. #if defined (MSDOS) || defined(_Windows) || (defined (ATARI) && defined(__PUREC__)) || defined(DOS386)
  392. #ifdef __PUREC__
  393.     setmatherr(purec_matherr);
  394. #endif
  395. #if defined (__TURBOC__) || defined (__PUREC__)
  396. #if !defined(DOSX286) && !defined(BROKEN_SIGINT)
  397.         (void) signal(SIGINT, tc_interrupt);    /* go there on interrupt char */
  398. #endif
  399. #else
  400. #ifdef __ZTC__
  401.         (void) signal(SIGINT, ztc_interrupt);
  402. #else
  403. #ifdef __EMX__
  404.         (void) signal(SIGINT, (void *)inter);    /* go there on interrupt char */
  405. #else
  406. #ifdef DJGPP
  407.         (void) signal(SIGINT, (SignalHandler)inter);    /* go there on interrupt char */
  408. #else
  409.                (void) signal(SIGINT, inter);
  410. #endif
  411. #endif
  412. #endif
  413. #endif
  414. #else /* MSDOS */
  415.         (void) signal(SIGINT, inter);    /* go there on interrupt char */
  416. #endif /* MSDOS */
  417. }
  418.  
  419.  
  420. /* Look for a gnuplot start-up file */
  421. load_rcfile()
  422. {
  423.     register FILE *plotrc;
  424.     char home[80]; 
  425.     char rcfile[sizeof(PLOTRC)+80];
  426.  
  427.     /* Look for a gnuplot init file in . or home directory */
  428. #ifdef vms
  429.     (void) strcpy(home,HOME);
  430. #else /* vms */
  431.     char *tmp_home=getenv(HOME);
  432.     char *p;    /* points to last char in home path, or to \0, if none */
  433.     char c='\0';/* character that should be added, or \0, if none */
  434.  
  435.  
  436.     if(tmp_home) {
  437.         strcpy(home,tmp_home);
  438.     if( strlen(home) ) p = &home[strlen(home)-1];
  439.     else           p = home;
  440. #if defined(MSDOS) || defined(ATARI) || defined( OS2 ) || defined(_Windows) || defined(DOS386)
  441.     if( *p!='\\' && *p!='\0' ) c='\\';
  442. #else
  443. #if defined(AMIGA_AC_5)
  444.     if( *p!='/' && *p!=':' && *p!='\0' ) c='/';
  445. #else /* that leaves unix */
  446.     c='/';
  447. #endif
  448. #endif
  449.     if(c) {
  450.         if(*p) p++;
  451.         *p++=c;
  452.         *p='\0';
  453.     }
  454.     }
  455. #endif /* vms */
  456.  
  457. #ifdef NOCWDRC
  458.     /* inhibit check of init file in current directory for security reasons */
  459.     {
  460. #else
  461.     (void) strcpy(rcfile, PLOTRC);
  462.     plotrc = fopen(rcfile,"r");
  463.     if (plotrc == (FILE *)NULL) {
  464. #endif
  465. #ifndef vms
  466.     if( tmp_home ) {
  467. #endif
  468.        (void) sprintf(rcfile, "%s%s", home, PLOTRC);
  469.        plotrc = fopen(rcfile,"r");
  470. #ifndef vms
  471.     } else
  472.         plotrc=NULL;
  473. #endif
  474.     }
  475.     if (plotrc)
  476.      load_file(plotrc, rcfile);
  477. }
  478.