home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / GPT34SRC / PLOT.C < prev    next >
C/C++ Source or Header  |  1993-05-25  |  14KB  |  490 lines

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