home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 334_02 / plot.c < prev    next >
Text File  |  1991-02-05  |  8KB  |  287 lines

  1. /* GNUPLOT - plot.c */
  2. /*
  3.  * Copyright (C) 1986, 1987, 1990   Thomas Williams, Colin Kelley
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software  is provided "as is" without express or implied warranty.
  16.  * 
  17.  *
  18.  * AUTHORS
  19.  * 
  20.  *   Original Software:
  21.  *     Thomas Williams,  Colin Kelley.
  22.  * 
  23.  *   Gnuplot 2.0 additions:
  24.  *       Russell Lang, Dave Kotz, John Campbell.
  25.  * 
  26.  * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
  27.  * 
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <setjmp.h>
  32. #include <signal.h>
  33. #include "plot.h"
  34. #include "setshow.h"
  35. #ifdef MSDOS
  36. #include <io.h>
  37. #endif
  38. #ifdef vms
  39. #include <unixio.h>
  40. #endif
  41. #ifdef __TURBOC__
  42. #include <graphics.h>
  43. #endif
  44.  
  45. extern char *getenv(),*strcat(),*strcpy(),*strncpy();
  46.  
  47. extern char input_line[];
  48. extern int c_token;
  49. extern FILE *outfile;
  50. extern int term;
  51.  
  52. BOOLEAN interactive = TRUE;    /* FALSE if stdin not a terminal */
  53. char *infile_name = NULL;    /* name of command file; NULL if terminal */
  54.  
  55. #ifndef STDOUT
  56. #define STDOUT 1
  57. #endif
  58.  
  59. jmp_buf env;
  60.  
  61. struct value *integer(),*complex();
  62.  
  63.  
  64. extern f_push(),f_pushc(),f_pushd(),f_call(),f_lnot(),f_bnot(),f_uminus()
  65.     ,f_lor(),f_land(),f_bor(),f_xor(),f_band(),f_eq(),f_ne(),f_gt(),f_lt(),
  66.     f_ge(),f_le(),f_plus(),f_minus(),f_mult(),f_div(),f_mod(),f_power(),
  67.     f_factorial(),f_bool(),f_jump(),f_jumpz(),f_jumpnz(),f_jtern();
  68.  
  69. extern f_real(),f_imag(),f_arg(),f_conjg(),f_sin(),f_cos(),f_tan(),f_asin(),
  70.     f_acos(),f_atan(),f_sinh(),f_cosh(),f_tanh(),f_int(),f_abs(),f_sgn(),
  71.     f_sqrt(),f_exp(),f_log10(),f_log(),f_besj0(),f_besj1(),f_besy0(),f_besy1(),
  72. #ifdef GAMMA
  73.     f_gamma(),
  74. #endif
  75.     f_floor(),f_ceil();
  76.  
  77.  
  78. struct ft_entry ft[] = {    /* built-in function table */
  79.  
  80. /* internal functions: */
  81.     {"push", f_push},    {"pushc", f_pushc},    {"pushd", f_pushd},
  82.     {"call", f_call},    {"lnot", f_lnot},    {"bnot", f_bnot},
  83.     {"uminus", f_uminus},                    {"lor", f_lor},
  84.     {"land", f_land},    {"bor", f_bor},        {"xor", f_xor},
  85.     {"band", f_band},    {"eq", f_eq},        {"ne", f_ne},
  86.     {"gt", f_gt},        {"lt", f_lt},        {"ge", f_ge},
  87.     {"le", f_le},        {"plus", f_plus},    {"minus", f_minus},
  88.     {"mult", f_mult},    {"div", f_div},        {"mod", f_mod},
  89.     {"power", f_power}, {"factorial", f_factorial},
  90.     {"bool", f_bool},    {"jump", f_jump},    {"jumpz", f_jumpz},
  91.     {"jumpnz",f_jumpnz},{"jtern", f_jtern},
  92.  
  93. /* standard functions: */
  94.     {"real", f_real},    {"imag", f_imag},    {"arg", f_arg},
  95.     {"conjg", f_conjg}, {"sin", f_sin},        {"cos", f_cos},
  96.     {"tan", f_tan},        {"asin", f_asin},    {"acos", f_acos},
  97.     {"atan", f_atan},    {"sinh", f_sinh},    {"cosh", f_cosh},
  98.     {"tanh", f_tanh},    {"int", f_int},        {"abs", f_abs},
  99.     {"sgn", f_sgn},        {"sqrt", f_sqrt},    {"exp", f_exp},
  100.     {"log10", f_log10},    {"log", f_log},        {"besj0", f_besj0},
  101.     {"besj1", f_besj1},    {"besy0", f_besy0},    {"besy1", f_besy1},
  102. #ifdef GAMMA
  103.      {"gamma", f_gamma},
  104. #endif
  105.     {"floor", f_floor},    {"ceil", f_ceil},
  106.     {NULL, NULL}
  107. };
  108.  
  109. static struct udvt_entry udv_pi = {NULL, "pi",FALSE};
  110.                                     /* first in linked list */
  111. struct udvt_entry *first_udv = &udv_pi;
  112. struct udft_entry *first_udf = NULL;
  113.  
  114.  
  115.  
  116. #ifdef vms
  117.  
  118. #define HOME "sys$login:"
  119.  
  120. #else /* vms */
  121. #ifdef MSDOS
  122.  
  123. #define HOME "GNUPLOT"
  124.  
  125. #else /* MSDOS */
  126.  
  127. #define HOME "HOME"
  128.  
  129. #endif /* MSDOS */
  130. #endif /* vms */
  131.  
  132. #ifdef unix
  133. #define PLOTRC ".gnuplot"
  134. #else
  135. #define PLOTRC "gnuplot.ini"
  136. #endif
  137.  
  138. #ifdef __TURBOC__
  139. void tc_interrupt()
  140. #else
  141. inter()
  142. #endif
  143. {
  144. #ifdef MSDOS
  145. #ifdef __TURBOC__
  146.     (void) signal(SIGINT, tc_interrupt);
  147. #else
  148.     void ss_interrupt();
  149.     (void) signal(SIGINT, ss_interrupt);
  150. #endif
  151. #else  /* MSDOS */
  152.     (void) signal(SIGINT, inter);
  153. #endif  /* MSDOS */
  154.     (void) signal(SIGFPE, SIG_DFL);    /* turn off FPE trapping */
  155.     if (term && term_init)
  156.         (*term_tbl[term].text)();    /* hopefully reset text mode */
  157.     (void) fflush(outfile);
  158.     (void) putc('\n',stderr);
  159.     longjmp(env, TRUE);        /* return to prompt */
  160. }
  161.  
  162.  
  163. main(argc, argv)
  164.     int argc;
  165.     char **argv;
  166. {
  167. /* Register the Borland Graphics Interface drivers. If they have been */
  168. /* included by the linker.                                            */
  169. #ifdef __TURBOC__
  170. registerfarbgidriver(CGA_driver_far);
  171. registerfarbgidriver(EGAVGA_driver_far);
  172. registerfarbgidriver(Herc_driver_far);
  173. registerfarbgidriver(ATT_driver_far);
  174. #endif
  175. #ifdef X11
  176.      { int n = X11_args(argc, argv); argv += n; argc -= n; }
  177. #endif 
  178.  
  179. #ifdef apollo
  180.     apollo_pfm_catch();
  181. #endif
  182.  
  183.     setbuf(stderr,(char *)NULL);
  184.     outfile = stdout;
  185.     (void) complex(&udv_pi.udv_value, Pi, 0.0);
  186.  
  187.      interactive = FALSE;
  188.      init_terminal();        /* can set term type if it likes */
  189.  
  190.      interactive = isatty(fileno(stdin));
  191.      if (argc > 1)
  192.       interactive = FALSE;
  193.  
  194.      if (interactive)
  195.       show_version();
  196.  
  197.     if (!setjmp(env)) {
  198.         /* first time */
  199.         interrupt_setup();
  200.         load_rcfile();
  201.  
  202.         if (interactive && term != 0)    /* not unknown */
  203.          fprintf(stderr, "\nTerminal type set to '%s'\n", 
  204.                 term_tbl[term].name);
  205.     } else {    
  206.         /* come back here from int_error() */
  207.         load_file_error();    /* if we were in load_file(), cleanup */
  208. #ifdef vms
  209.         /* after catching interrupt */
  210.         /* VAX stuffs up stdout on SIGINT while writing to stdout,
  211.           so reopen stdout. */
  212.         if (outfile == stdout) {
  213.            if ( (stdout = freopen("SYS$OUTPUT","w",stdout))  == NULL) {
  214.               /* couldn't reopen it so try opening it instead */
  215.               if ( (stdout = fopen("SYS$OUTPUT","w"))  == NULL) {
  216.                  /* don't use int_error here - causes infinite loop! */
  217.                  fprintf(stderr,"Error opening SYS$OUTPUT as stdout\n");
  218.               }
  219.            }
  220.            outfile = stdout;
  221.         }
  222. #endif                    /* VMS */
  223.         if (!interactive)
  224.          done(IO_ERROR);            /* exit on non-interactive error */
  225.     }
  226.  
  227.      if (argc > 1) {
  228.         /* load filenames given as arguments */
  229.         while (--argc > 0) {
  230.            ++argv;
  231.            c_token = NO_CARET; /* in case of file not found */
  232.            load_file(fopen(*argv,"r"), *argv);    
  233.         }
  234.     } else {
  235.         /* take commands from stdin */
  236.         while(TRUE)
  237.          com_line();
  238.     }
  239.  
  240.      done(IO_SUCCESS);
  241. }
  242.  
  243. /* Set up to catch interrupts */
  244. interrupt_setup()
  245. {
  246. #ifdef MSDOS
  247. #ifdef __TURBOC__
  248.         (void) signal(SIGINT, tc_interrupt);    /* go there on interrupt char */
  249. #else
  250.         void ss_interrupt();
  251.         save_stack();                /* work-around for MSC 4.0/MSDOS 3.x bug */
  252.         (void) signal(SIGINT, ss_interrupt);
  253. #endif
  254. #else /* MSDOS */
  255.         (void) signal(SIGINT, inter);    /* go there on interrupt char */
  256. #endif /* MSDOS */
  257. }
  258.  
  259.  
  260. /* Look for a gnuplot start-up file */
  261. load_rcfile()
  262. {
  263.     register FILE *plotrc;
  264.     static char home[80];
  265.     static char rcfile[sizeof(PLOTRC)+80];
  266.  
  267.     /* Look for a gnuplot init file in . or home directory */
  268. #ifdef vms
  269.     (void) strcpy(home,HOME);
  270. #else
  271.     (void) strcat(strcpy(home,getenv(HOME)),"/");
  272. #endif                    /* vms */
  273. #ifdef NOCWDRC
  274.     /* inhibit check of init file in current directory for security reasons */
  275.     {
  276. #else
  277.     (void) strcpy(rcfile, PLOTRC);
  278.     plotrc = fopen(rcfile,"r");
  279.     if (plotrc == (FILE *)NULL) {
  280. #endif
  281.        (void) sprintf(rcfile, "%s%s", home, PLOTRC);
  282.        plotrc = fopen(rcfile,"r");
  283.     }
  284.     if (plotrc)
  285.      load_file(plotrc, rcfile);
  286. }
  287.