home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / gnuplot.lha / gnuplot / src / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-28  |  116.5 KB  |  4,414 lines

  1. #ifndef lint
  2. static char    *RCSid = "$Id: command.c%v 3.50.1.15 1993/08/21 15:23:42 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - command.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  * 
  10.  * Permission to use, copy, and distribute this software and its documentation
  11.  * for any purpose with or without fee is hereby granted, provided that the
  12.  * above copyright notice appear in all copies and that both that copyright
  13.  * notice and this permission notice appear in supporting documentation.
  14.  * 
  15.  * Permission to modify the software is granted, but not the right to distribute
  16.  * the modified code.  Modifications are to be distributed as patches to
  17.  * released version.
  18.  * 
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  * 
  22.  * AUTHORS
  23.  * 
  24.  * Original Software: Thomas Williams,  Colin Kelley.
  25.  * 
  26.  * Gnuplot 2.0 additions: Russell Lang, Dave Kotz, John Campbell.
  27.  * 
  28.  * Gnuplot 3.0 additions: Gershon Elber and many others.
  29.  * 
  30.  *   Nonlinear Least squares fit:
  31.  *     Carsten Grammes
  32.  * 
  33.  * Changes:
  34.  * 
  35.  * Feb 5, 1992    Jack Veenstra    (veenstra@cs.rochester.edu) Added support to
  36.  * filter data values read from a file through a user-defined function before
  37.  * plotting. The keyword "thru" was added to the "plot" command. Example
  38.  * syntax: f(x) = x / 100 plot "test.data" thru f(x) This example divides all
  39.  * the y values by 100 before plotting. The filter function processes the
  40.  * data before any log-scaling occurs. This capability should be generalized
  41.  * to filter x values as well and a similar feature should be added to the
  42.  * "splot" command.
  43.  * 
  44.  * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
  45.  * Added user-specified bases for log scaling.
  46.  * 
  47.  * There is a mailing list for gnuplot users. Note, however, that the
  48.  * newsgroup 
  49.  *    comp.graphics.gnuplot 
  50.  * is identical to the mailing list (they
  51.  * both carry the same set of messages). We prefer that you read the
  52.  * messages through that newsgroup, to subscribing to the mailing list.
  53.  * (If you can read that newsgroup, and are already on the mailing list,
  54.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  55.  * removed from the mailing list.)
  56.  *
  57.  * The address for mailing to list members is
  58.  *       info-gnuplot@dartmouth.edu
  59.  * and for mailing administrative requests is 
  60.  *       info-gnuplot-request@dartmouth.edu
  61.  * The mailing list for bug reports is 
  62.  *       bug-gnuplot@dartmouth.edu
  63.  * Send bug reports regarding the fit to
  64.  *       cagr@rz.uni-sb.de
  65.  * The list of those interested in beta-test versions is
  66.  *       info-gnuplot-beta@dartmouth.edu
  67.  */
  68.  
  69. #include <stdio.h>
  70. #include <math.h>
  71. #include <ctype.h>
  72.  
  73. #ifdef AMIGA_AC_5
  74. #include <time.h>
  75. void            sleep();    /* defined later */
  76. #endif
  77.  
  78. #ifdef OS2
  79. #include <setjmp.h>
  80. extern jmp_buf env;       /* from plot.c */
  81. #endif
  82.  
  83. #if defined(MSDOS) || defined(DOS386)
  84. #ifdef DJGPP
  85. #include <dos.h>
  86. #else
  87. #include <process.h>
  88. #endif
  89.  
  90. #ifdef __ZTC__
  91. #define P_WAIT 0
  92. #include <time.h>        /* usleep() */
  93. #else
  94.  
  95. #ifdef __TURBOC__
  96. #ifndef _Windows
  97. #include <dos.h>        /* sleep() */
  98. #include <conio.h>
  99. #include <dir.h>    /* setdisk() */
  100. extern unsigned _stklen = 16394;/* increase stack size */
  101. #endif
  102.  
  103. #else                /* must be MSC */
  104. #if !defined(__EMX__) && !defined(DJGPP)
  105. #ifdef __MSC__
  106. #include <direct.h>        /* for _chdrive() */
  107. #endif
  108. #include <time.h>        /* kludge to provide sleep() */
  109. void            sleep();    /* defined later */
  110. #endif                /* !__EMX__ && !DJGPP */
  111. #endif                /* TURBOC */
  112. #endif                /* ZTC */
  113.  
  114. #endif                /* MSDOS */
  115.  
  116. #ifdef ATARI
  117. #ifdef __PUREC__
  118. #include <ext.h>
  119. #include <tos.h>
  120. #include <aes.h>
  121. #else
  122. #include <osbind.h>
  123. #include <aesbind.h>
  124. #endif /* __PUREC__ */
  125. #endif /* ATARI */
  126.  
  127. #ifdef AMIGA_SC_6_1
  128. #include <proto/dos.h>
  129. void            sleep();
  130. #endif                /* AMIGA_SC_6_1 */
  131.  
  132. #include "plot.h"
  133. #include "setshow.h"
  134. #include "fit.h"
  135. #ifndef _Windows
  136. #include "help.h"
  137. #else
  138. #define MAXSTR 255
  139. #endif
  140.  
  141. #ifndef STDOUT
  142. #define STDOUT 1
  143. #endif
  144.  
  145. #ifndef HELPFILE
  146. #if defined( MSDOS ) || defined( OS2 ) || defined(DOS386)
  147. #define HELPFILE "gnuplot.gih"
  148. #else
  149. #ifdef AMIGA_SC_6_1
  150. #define HELPFILE "S:gnuplot.gih"
  151. #else
  152. #define HELPFILE "docs/gnuplot.gih"    /* changed by makefile */
  153. #endif                /* AMIGA_SC_6_1 */
  154. #endif
  155. #endif                /* HELPFILE */
  156.  
  157. #ifdef _Windows
  158. #include <windows.h>
  159. #include <setjmp.h>
  160. #ifdef __MSC__
  161. #include <malloc.h>
  162. #else
  163. #include <alloc.h>
  164. #include <dir.h>    /* setdisk() */
  165. #endif
  166. #include "win/wgnuplib.h"
  167. void sleep();
  168. extern TW textwin;
  169. extern jmp_buf FAR env;       /* from plot.c */
  170. extern LPSTR winhelpname;
  171. extern void screen_dump(void);    /* in term/win.trm */
  172. extern int Pause(LPSTR mess); /* in winmain.c */
  173. #endif
  174.  
  175. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  176.  
  177. /*
  178.  * instead of <strings.h>
  179.  */
  180.  
  181. #ifndef ATARI
  182. #ifdef _Windows
  183. #include <string.h>
  184. #include <stdlib.h>
  185. #else
  186. #ifndef AMIGA_SC_6_1
  187. extern char    *gets(), *getenv();
  188. #ifdef sequent
  189. extern char    *strcpy(), *strncpy(), *strcat(), *index();
  190. #else
  191. extern char    *strcpy(), *strncpy(), *strcat(), *strchr();
  192. #endif
  193. extern int      strlen();
  194. extern int      strcmp();
  195. extern double   atof();
  196. #endif /* !AMIGA_SC_6_1 */
  197. #endif
  198. #else
  199. #ifdef __PUREC__
  200. /*
  201.  * a substitute for PureC's buggy sscanf.
  202.  * this uses the normal sscanf and fixes the following bugs:
  203.  * - whitespace in format matches whitespace in string, but doesn't
  204.  *   require any. ( "%f , %f" scans "1,2" correctly )
  205.  * - the ignore value feature works (*). this created an address error
  206.  *   in PureC.
  207.  */
  208.  
  209. #include <stdarg.h>
  210. #include <string.h>
  211.  
  212. int purec_sscanf( const char *string, const char *format, ... )
  213. {
  214.   va_list args;
  215.   int cnt=0;
  216.   char onefmt[256];
  217.   char buffer[256];
  218.   const char *f=format;
  219.   const char *s=string;
  220.   char *f2;
  221.   char ch;
  222.   int ignore;
  223.   void *p;
  224.   int *ip;
  225.   int pos;
  226.  
  227.   va_start(args,format);
  228.   while( *f && *s ) {
  229.     ch=*f++;
  230.     if( ch!='%' ) {
  231.       if(isspace(ch)) {
  232.         /* match any number of whitespace */
  233.         while(isspace(*s)) s++;
  234.       } else {
  235.         /* match exactly the character ch */
  236.         if( *s!=ch ) goto finish;
  237.         s++;
  238.       }
  239.     } else {
  240.       /* we have got a '%' */
  241.       ch=*f++;
  242.       if( ch=='%' ) {
  243.         /* match exactly % */
  244.         if( *s!=ch ) goto finish;
  245.         s++;
  246.       } else {
  247.         f2=onefmt;
  248.         *f2++='%';
  249.         *f2++=ch;
  250.         ignore=0;
  251.         if( ch=='*' ) {
  252.           ignore=1;
  253.           ch=f2[-1]=*f++;
  254.         }
  255.         while( isdigit(ch) ) {
  256.           ch=*f2++=*f++;
  257.         }
  258.         if( ch=='l' || ch=='L' || ch=='h' ) {
  259.           ch=*f2++=*f++;
  260.         }
  261.         switch(ch) {
  262.           case '[':
  263.             while( ch && ch!=']' ) {
  264.               ch=*f2++=*f++;
  265.             }
  266.             if( !ch ) goto error;
  267.             break;
  268.           case 'e':
  269.           case 'f':
  270.           case 'g':
  271.           case 'd':
  272.           case 'o':
  273.           case 'i':
  274.           case 'u':
  275.           case 'x':
  276.           case 'c':
  277.           case 's':
  278.           case 'p':
  279.           case 'n': /* special case handled below */
  280.             break;
  281.           default:
  282.             goto error;
  283.         }
  284.         if( ch!='n' ) {
  285.           strcpy(f2,"%n");
  286.           if( ignore ) {
  287.             p=buffer;
  288.           } else {
  289.             p=va_arg(args,void *);
  290.           }
  291.           switch( sscanf( s, onefmt, p, &pos ) ) {
  292.             case EOF: goto error;
  293.             case  0 : goto finish;
  294.           }
  295.           if( !ignore ) cnt++;
  296.           s+=pos;
  297.         } else {
  298.           if( !ignore ) {
  299.             ip=va_arg(args,int *);
  300.             *ip=(int)(s-string);
  301.           }
  302.         }
  303.       }
  304.     }
  305.   }
  306.  
  307.   if( !*f ) goto finish;
  308.  
  309. error:
  310.   cnt=EOF;
  311. finish:
  312.   va_end(args);
  313.   return cnt;
  314. }
  315.  
  316. /* use the substitute now. I know this is dirty trick, but it works. */
  317. #define sscanf purec_sscanf
  318.  
  319. #endif /* __PUREC__ */
  320. #endif /* ATARI */
  321.  
  322. /*
  323.  * Only reference to contours library.
  324.  */
  325. extern struct gnuplot_contours *contour();
  326.  
  327. #ifdef OS2
  328.  /* emx has getcwd, chdir that can handle drive names */
  329. #define getcwd _getcwd2
  330. #define chdir  _chdir2
  331. #endif /* OS2 */
  332.  
  333. #if defined(unix) && !defined(hpux)
  334. #ifdef GETCWD
  335. extern char    *getcwd();    /* some Unix's use getcwd */
  336. #else
  337. extern char    *getwd();    /* most Unix's use getwd */
  338. #endif
  339. #else
  340. #ifdef DJGPP
  341. extern char    *getwd();    /* DJGPP acts like Unix here */
  342. #else
  343. extern char    *getcwd();    /* Turbo C, MSC, EMX, OS2 and VMS use getcwd */
  344. #endif
  345. #endif
  346.  
  347. #ifdef vms
  348. int             vms_vkid;    /* Virtual keyboard id */
  349. #endif
  350.     
  351. static FILE *data_fp=NULL;    /* != means file still open */
  352. static TBOOLEAN more_data_fp=FALSE;  /* And this explicitly says so. */
  353.  
  354. #if defined(unix) || defined(PIPES)
  355. extern FILE    *popen();
  356. static TBOOLEAN  pipe_open = FALSE;
  357. #endif
  358.  
  359. extern int      chdir();
  360.  
  361. extern double   magnitude(), angle(), real(), imag();
  362. extern struct value *const_express(), *pop(), *Gcomplex();
  363. extern struct at_type *temp_at(), *perm_at();
  364. extern struct udft_entry *add_udf();
  365. extern struct udvt_entry *add_udv();
  366. extern void     squash_spaces();
  367. extern void     lower_case();
  368.  
  369. /* local functions */
  370. static enum coord_type adjustlog();
  371.  
  372. extern TBOOLEAN  interactive;    /* from plot.c */
  373.  
  374. /* input data, parsing variables */
  375. struct lexical_unit token[MAX_TOKENS];
  376. char            input_line[MAX_LINE_LEN + 1] = "";
  377. int             num_tokens, c_token;
  378. int             inline_num = 0;    /* input line number */
  379.  
  380. char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  381.  
  382. /* the curves/surfaces of the plot */
  383. struct curve_points *first_plot = NULL;
  384. struct surface_points *first_3dplot = NULL;
  385. static struct udft_entry plot_func;
  386. struct udft_entry *dummy_func;
  387.  
  388. /* jev -- for passing data thru user-defined function */
  389. static struct udft_entry ydata_func;
  390.  
  391. /* support for replot command */
  392. char            replot_line[MAX_LINE_LEN + 1] = "";
  393. static int      plot_token;    /* start of 'plot' command */
  394.  
  395. /* If last plot was a 3d one. */
  396. TBOOLEAN         is_3d_plot = FALSE;
  397.  
  398. #define min(a,b)    ((a)>(b) ? (b) : (a))
  399. #define Inc_c_token if (++c_token >= num_tokens)    \
  400.                         int_error ("Syntax error", c_token);
  401.  
  402. com_line()
  403. {
  404.    if (read_line(PROMPT))
  405.        return(1);
  406.  
  407.     /* So we can flag any new output: if false at time of error, */
  408.     /* we reprint the command line before printing caret. */
  409.     /* TRUE for interactive terminals, since the command line is typed. */
  410.     /* FALSE for non-terminal stdin, so command line is printed anyway. */
  411.     /* (DFK 11/89) */
  412.     screen_ok = interactive;
  413.  
  414.     if (do_line())
  415.         return(1);
  416.      else
  417.         return(0);
  418. }
  419.  
  420.  
  421. do_line()
  422. {                /* also used in load_file */
  423.     if (is_system(input_line[0])) {
  424.     do_system();
  425.     (void) fputs("!\n", stderr);
  426.     return(0);
  427.     }
  428.     num_tokens = scanner(input_line);
  429.     c_token = 0;
  430.     while (c_token < num_tokens) {
  431.     if (command())
  432.            return(1);
  433.     if (c_token < num_tokens)    /* something after command */
  434.         if (equals(c_token, ";"))
  435.         c_token++;
  436.         else
  437.         int_error("';' expected", c_token);
  438.     }
  439.     return(0);
  440. }
  441.  
  442.  
  443.  
  444. command()
  445. {
  446.     FILE *fp, *lf_top();
  447.     int             i;
  448.     char            sv_file[MAX_LINE_LEN + 1];
  449. #if defined(__ZTC__)
  450.     unsigned dummy; /* it's a parameter needed for dos_setdrive */
  451. #endif
  452.     /* string holding name of save or load file */
  453.  
  454.     for (i = 0; i < MAX_NUM_VAR; i++)
  455.     c_dummy_var[i][0] = '\0';    /* no dummy variables */
  456.  
  457.     if (is_definition(c_token))
  458.     define();
  459.     else if (almost_equals(c_token, "h$elp") || equals(c_token, "?")) {
  460.     c_token++;
  461.     do_help();
  462.     } else if (almost_equals(c_token, "test")) {
  463.     c_token++;
  464.     test_term();
  465.     } else if (almost_equals(c_token, "scr$eendump")) {
  466.     c_token++;
  467. #ifdef _Windows
  468.     screen_dump();
  469. #else
  470.     fputs("screendump not implemented\n",stderr);
  471. #endif
  472.     } else if (almost_equals(c_token, "pa$use")) {
  473.     struct value    a;
  474.     int             stime, text = 0;
  475.     char            buf[MAX_LINE_LEN + 1];
  476.  
  477.     c_token++;
  478.     stime = (int) real(const_express(&a));
  479.     buf[0]='\0';
  480.     if (!(END_OF_COMMAND)) {
  481.         if (!isstring(c_token))
  482.         int_error("expecting string", c_token);
  483.         else {
  484.         quotel_str(buf, c_token);
  485. #ifdef _Windows
  486.         if (stime>=0)
  487. #endif
  488. #ifdef OS2
  489.                 if( strcmp(term_tbl[term].name, "pm" )!=0 || stime >=0 )
  490. #endif
  491.         (void) fprintf(stderr, "%s", buf);
  492.         text = 1;
  493.         }
  494.     }
  495.     if (stime < 0)
  496. #ifdef _Windows
  497.         {
  498.             if (!Pause(buf))
  499.                  longjmp(env, TRUE); /* bail out to command line */
  500.          }
  501. #else
  502. #ifdef OS2
  503.         if( strcmp(term_tbl[term].name, "pm" )==0 && stime < 0 )
  504.         {
  505.             int rc ;
  506.             if( (rc=PM_pause( buf ))==0 ) longjmp(env,TRUE) ;
  507.             else if( rc==2 ) { 
  508.         (void) fprintf(stderr, "%s", buf);
  509.         text = 1;
  510.                 (void) fgets(buf, MAX_LINE_LEN, stdin);
  511.                 }
  512.         }
  513. #else
  514.         (void) fgets(buf, MAX_LINE_LEN, stdin);
  515.     /* Hold until CR hit. */
  516. #endif /*OS2*/
  517. #endif
  518. #ifdef __ZTC__
  519.     if (stime > 0)
  520.         usleep((unsigned long) stime);
  521. #else
  522.     if (stime > 0)
  523.         sleep((unsigned int) stime);
  524. #endif
  525.     if (text != 0 && stime >= 0)
  526.         (void) fprintf(stderr, "\n");
  527.     c_token++;
  528.     screen_ok = FALSE;
  529.     }
  530.     else if (almost_equals(c_token, "pr$int")) {
  531.     struct value    a;
  532.  
  533.     c_token++;
  534.     (void) const_express(&a);
  535.     (void) putc('\t', stderr);
  536.     disp_value(stderr, &a);
  537.     (void) putc('\n', stderr);
  538.     screen_ok = FALSE;
  539.     } else if (almost_equals(c_token,"fit")) {
  540.     register int tmp_token = c_token++,
  541.              len,
  542.              xcol=1,
  543.              ycol=2,
  544.              dycol=0;        /* default: no weights */
  545.     char        fitfunction[80],
  546.             datafile[80],
  547.             paramsfile[256],
  548.             tmps[80];
  549.  
  550.     /*
  551.      * Syntax: fit f(x) "datafile" <using x:y<:dy>> "paramsfile"
  552.      * or:       fit f(x) "datafile" <using x:y<:dy>> via p1, p2,...
  553.      */
  554.     while ( !isstring(++tmp_token) && tmp_token<num_tokens)    ;
  555.     strncpy (fitfunction, &input_line[token[c_token].start_index],
  556.         len = min(token[tmp_token].start_index - token[c_token].start_index,
  557.         sizeof(fitfunction)));
  558.     fitfunction[len] = '\0';
  559.     if ( !*fitfunction )
  560.         int_error ("Fitfunction expected", c_token);
  561.     if ( !isstring(c_token = tmp_token) )
  562.         int_error ("Data file expected", c_token);
  563.     quote_str (datafile, c_token);
  564.  
  565.     Inc_c_token
  566.     if ( !strnicmp(input_line+token[c_token].start_index,"using",5) ) {
  567.         Inc_c_token
  568.         if ( !isnumber (c_token) )
  569.         int_error ("xcol:ycol<:yerr> expected", c_token);
  570.         copy_str (tmps, c_token);
  571.         xcol = atoi(tmps);
  572.         Inc_c_token
  573.         copy_str (tmps, c_token);
  574.         if ( strcmp (tmps,":") )
  575.         int_error ("xcol:ycol<:yerr> expected", c_token);
  576.         Inc_c_token
  577.         copy_str (tmps, c_token);
  578.         if ( !isnumber (c_token) )
  579.         int_error ("xcol:ycol<:yerr> expected", c_token);
  580.         ycol = atoi(tmps);
  581.         Inc_c_token
  582.         copy_str (tmps, c_token);
  583.         if ( !strcmp (tmps,":") ) {
  584.         Inc_c_token
  585.         copy_str (tmps, c_token);
  586.         if ( !isnumber (c_token) )
  587.             int_error ("xcol:ycol<:yerr> expected", c_token);
  588.         dycol = atoi (tmps);
  589.         Inc_c_token
  590.         }
  591.     }
  592.     if ( !strnicmp(&input_line[token[c_token].start_index], "via ", 4) )
  593.         strcpy (paramsfile, &input_line[token[c_token].start_index]);
  594.     else {
  595.         if ( !isstring(c_token) )
  596.         int_error ("Parameter file expected", c_token);
  597.         quote_str (paramsfile, c_token);
  598.         if ( c_token < num_tokens-1 )
  599.         int_error ("extra input causes syntax error", ++c_token);
  600.     }
  601.     do_fit (fitfunction, datafile, xcol, ycol, dycol, paramsfile);
  602.     }
  603.     else if (almost_equals(c_token,"up$date")) {
  604.         char tmps[80];
  605.                 if ( !isstring(++c_token) )
  606.             int_error ("Parameter filename expected", c_token);
  607.         quote_str (tmps, c_token++);
  608.         update (tmps);
  609.         }
  610.     else if (almost_equals(c_token, "p$lot")) {
  611.     plot_token = c_token++;
  612. #ifdef _Windows
  613.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  614. #endif
  615.     plotrequest();
  616. #ifdef _Windows
  617.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  618. #endif
  619.     } else if (almost_equals(c_token, "sp$lot")) {
  620.     plot_token = c_token++;
  621. #ifdef _Windows
  622.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  623. #endif
  624.     plot3drequest();
  625. #ifdef _Windows
  626.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  627. #endif
  628.     } else if (almost_equals(c_token, "rep$lot")) {
  629.     if (replot_line[0] == '\0')
  630.         int_error("no previous plot", c_token);
  631.     c_token++;
  632. #ifdef _Windows
  633.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  634. #endif
  635.     replotrequest();
  636. #ifdef _Windows
  637.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  638. #endif
  639.     } else if (almost_equals(c_token, "se$t"))
  640.     set_command();
  641.     else if (almost_equals(c_token, "sh$ow"))
  642.     show_command();
  643.     else if (almost_equals(c_token, "cl$ear")) {
  644.     if (!term_init) {
  645.         (*term_tbl[term].init) ();
  646.         term_init = TRUE;
  647.     }
  648.     (*term_tbl[term].graphics) ();
  649.     (*term_tbl[term].text) ();
  650.     (void) fflush(outfile);
  651.     screen_ok = FALSE;
  652.     c_token++;
  653.     } else if (almost_equals(c_token, "she$ll")) {
  654.     do_shell();
  655.     screen_ok = FALSE;
  656.     c_token++;
  657.     } else if (almost_equals(c_token, "sa$ve")) {
  658.     if (almost_equals(++c_token, "f$unctions")) {
  659.         if (!isstring(++c_token))
  660.         int_error("expecting filename", c_token);
  661.         else {
  662.         quote_str(sv_file, c_token);
  663.         save_functions(fopen(sv_file, "w"));
  664.         }
  665.     } else if (almost_equals(c_token, "v$ariables")) {
  666.         if (!isstring(++c_token))
  667.         int_error("expecting filename", c_token);
  668.         else {
  669.         quote_str(sv_file, c_token);
  670.         save_variables(fopen(sv_file, "w"));
  671.         }
  672.     } else if (almost_equals(c_token, "s$et")) {
  673.         if (!isstring(++c_token))
  674.         int_error("expecting filename", c_token);
  675.         else {
  676.         quote_str(sv_file, c_token);
  677.         save_set(fopen(sv_file, "w"));
  678.         }
  679.     } else if (isstring(c_token)) {
  680.         quote_str(sv_file, c_token);
  681.         save_all(fopen(sv_file, "w"));
  682.     } else {
  683.         int_error(
  684.              "filename or keyword 'functions', 'variables', or 'set' expected",
  685.              c_token);
  686.     }
  687.     c_token++;
  688.     } else if (almost_equals(c_token, "l$oad")) {
  689.     if (!isstring(++c_token))
  690.         int_error("expecting filename", c_token);
  691.     else {
  692.         quote_str(sv_file, c_token);
  693.         load_file(fp=fopen(sv_file, "r"), sv_file);
  694.         /* input_line[] and token[] now destroyed! */
  695.         c_token = num_tokens = 0;
  696.     }
  697.     } else if (almost_equals(c_token,"rer$ead")) {
  698.             fp = lf_top();
  699.             if (fp != (FILE *)NULL) rewind(fp);
  700.             c_token++;
  701.     } else if (almost_equals(c_token, "cd")) {
  702.     if (!isstring(++c_token))
  703.         int_error("expecting directory name", c_token);
  704.     else {
  705.         quotel_str(sv_file, c_token);
  706. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  707.         if (!((strlen(sv_file)==2) && isalpha(sv_file[0]) && (sv_file[1]==':')))
  708. #endif
  709.         if (chdir(sv_file)) {
  710.         int_error("Can't change to this directory", c_token);
  711.         }
  712. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  713.         if (isalpha(sv_file[0]) && (sv_file[1]==':')) {
  714. #ifdef ATARI
  715.         (void)Dsetdrv(toupper(sv_file[0])-'A');
  716. #endif
  717.  
  718. #if defined(__ZTC__)
  719.       (void)dos_setdrive(toupper(sv_file[0]) - 'A' + 1, &dummy);
  720. #endif
  721.  
  722. #if defined(MSDOS) && defined(__EMX__)
  723.         (void)_chdrive(toupper(sv_file[0]));
  724. #endif
  725. #if defined(__MSC__)
  726.         (void)_chdrive(toupper(sv_file[0])-'A');
  727. #endif
  728. #if (defined(MSDOS) || defined(_Windows)) && defined(__TURBOC__)
  729.         (void) setdisk(toupper(sv_file[0])-'A');
  730. #endif
  731. #ifdef DJGPP
  732.         { union REGS r;
  733.             r.h.ah = 0x0e;
  734.             r.x.dx = toupper(sv_file[0])-'A';
  735.             intdos(&r, &r);
  736.         }
  737. #endif
  738.         }
  739. #endif
  740.         c_token++;
  741.     }
  742.     } else if (almost_equals(c_token, "pwd")) {
  743. #if defined(unix) && !defined(hpux)
  744. #ifdef GETCWD
  745.     (void) getcwd(sv_file, MAX_ID_LEN);    /* some Unix's use getcwd */
  746. #else
  747.     (void) getwd(sv_file);    /* most Unix's use getwd */
  748. #endif
  749. #else
  750. #ifdef __EMX__
  751.     (void) _getcwd2(sv_file, MAX_ID_LEN);
  752. #else
  753.     /* Turbo C and VMS have getcwd() */
  754.     (void) getcwd(sv_file, MAX_ID_LEN);
  755. #endif
  756. #endif
  757. #ifdef DJGPP
  758.     { union REGS r;
  759.         r.h.ah = 0x19;
  760.         intdos(&r, &r);
  761.         fprintf(stderr, "%c:", r.h.al + 'a');
  762.     }
  763. #endif
  764.     fprintf(stderr, "%s\n", sv_file);
  765.     c_token++;
  766.     } else if (almost_equals(c_token, "ex$it") ||
  767.            almost_equals(c_token, "q$uit")) {
  768.     return(1);
  769.     } else if (!equals(c_token, ";")) {    /* null statement */
  770.     int_error("invalid command", c_token);
  771.     }
  772.     return(0);
  773. }
  774.  
  775. replotrequest()
  776. {
  777.     char            str[MAX_LINE_LEN + 1];
  778.     if (equals(c_token, "["))
  779.     int_error("cannot set range with replot", c_token);
  780.     if (!END_OF_COMMAND) {
  781.     capture(str, c_token, num_tokens - 1);
  782.     if ((strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN - 1) {
  783.         (void) strcat(replot_line, ",");
  784.         (void) strcat(replot_line, str);
  785.     } else {
  786.         int_error("plot line too long with replot arguments", c_token);
  787.     }
  788.     }
  789.     (void) strcpy(input_line, replot_line);
  790.     screen_ok = FALSE;
  791.     num_tokens = scanner(input_line);
  792.     c_token = 1;        /* skip the 'plot' part */
  793.     is_3d_plot ? plot3drequest() : plotrequest();
  794. }
  795.  
  796.  
  797. plotrequest()
  798. /*
  799.  * In the parametric case we can say plot [a= -4:4] [-2:2] [-1:1] sin(a),a**2
  800.  * while in the non-parametric case we would say only plot [b= -2:2] [-1:1]
  801.  * sin(b)
  802.  */
  803. {
  804.     TBOOLEAN         changed;
  805.     int             dummy_token = -1;
  806.  
  807.     is_3d_plot = FALSE;
  808.  
  809.     if (parametric && strcmp(dummy_var[0], "u") == 0)
  810.     strcpy(dummy_var[0], "t");
  811.  
  812.     autoscale_lt = autoscale_t;
  813.     autoscale_lx = autoscale_x;
  814.     autoscale_ly = autoscale_y;
  815.  
  816.     if (!term)            /* unknown */
  817.     int_error("use 'set term' to set terminal type first", c_token);
  818.  
  819.     if (equals(c_token, "[")) {
  820.     c_token++;
  821.     if (isletter(c_token)) {
  822.         if (equals(c_token + 1, "=")) {
  823.         dummy_token = c_token;
  824.         c_token += 2;
  825.         } else {
  826.         /* oops; probably an expression with a variable. */
  827.         /* Parse it as an xmin expression. */
  828.         /* used to be: int_error("'=' expected",c_token); */
  829.         }
  830.     }
  831.     changed = parametric ? load_range(&tmin, &tmax) : load_range(&xmin, &xmax);
  832.     if (!equals(c_token, "]"))
  833.         int_error("']' expected", c_token);
  834.     c_token++;
  835.     if (changed) {
  836.         if (parametric)
  837.         autoscale_lt = FALSE;
  838.         else
  839.         autoscale_lx = FALSE;
  840.     }
  841.     }
  842.     if (parametric && equals(c_token, "[")) {    /* set optional x ranges */
  843.     c_token++;
  844.     changed = load_range(&xmin, &xmax);
  845.     if (!equals(c_token, "]"))
  846.         int_error("']' expected", c_token);
  847.     c_token++;
  848.     if (changed)
  849.         if(parametric)
  850.             autoscale_lt = FALSE;
  851.         else
  852.                 autoscale_lx = FALSE;
  853.     }
  854.     if (equals(c_token, "[")) {    /* set optional y ranges */
  855.     c_token++;
  856.     changed = load_range(&ymin, &ymax);
  857.     if (!equals(c_token, "]"))
  858.         int_error("']' expected", c_token);
  859.     c_token++;
  860.     if (changed)
  861.         autoscale_ly = FALSE;
  862.     }
  863.     /* use the default dummy variable unless changed */
  864.     if (dummy_token >= 0)
  865.     copy_str(c_dummy_var[0], dummy_token);
  866.     else
  867.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  868.  
  869.     eval_plots();
  870. }
  871.  
  872. plot3drequest()
  873. /*
  874.  * in the parametric case we would say splot [u= -Pi:Pi] [v= 0:2*Pi] [-1:1]
  875.  * [-1:1] [-1:1] sin(v)*cos(u),sin(v)*cos(u),sin(u) in the non-parametric
  876.  * case we would say only splot [x= -2:2] [y= -5:5] sin(x)*cos(y)
  877.  * 
  878.  */
  879. {
  880.     TBOOLEAN         changed;
  881.     int             dummy_token0 = -1, dummy_token1 = -1;
  882.  
  883.     is_3d_plot = TRUE;
  884.  
  885.     if (parametric && strcmp(dummy_var[0], "t") == 0) {
  886.     strcpy(dummy_var[0], "u");
  887.     strcpy(dummy_var[1], "v");
  888.     }
  889.     autoscale_lx = autoscale_x;
  890.     autoscale_ly = autoscale_y;
  891.     autoscale_lz = autoscale_z;
  892.  
  893.     if (!term)            /* unknown */
  894.     int_error("use 'set term' to set terminal type first", c_token);
  895.  
  896.     if (equals(c_token, "[")) {
  897.     c_token++;
  898.     if (isletter(c_token)) {
  899.         if (equals(c_token + 1, "=")) {
  900.         dummy_token0 = c_token;
  901.         c_token += 2;
  902.         } else {
  903.         /* oops; probably an expression with a variable. */
  904.         /* Parse it as an xmin expression. */
  905.         /* used to be: int_error("'=' expected",c_token); */
  906.         }
  907.     }
  908.     changed = parametric ? load_range(&umin, &umax) : load_range(&xmin, &xmax);
  909.     if (!equals(c_token, "]"))
  910.         int_error("']' expected", c_token);
  911.     c_token++;
  912.     if (changed)
  913.         if(parametric) 
  914.             autoscale_lu = FALSE;
  915.         else
  916.             autoscale_lx = FALSE;
  917.     }
  918.     if (equals(c_token, "[")) {
  919.     c_token++;
  920.     if (isletter(c_token)) {
  921.         if (equals(c_token + 1, "=")) {
  922.         dummy_token1 = c_token;
  923.         c_token += 2;
  924.         } else {
  925.         /* oops; probably an expression with a variable. */
  926.         /* Parse it as an xmin expression. */
  927.         /* used to be: int_error("'=' expected",c_token); */
  928.         }
  929.     }
  930.     changed = parametric ? load_range(&vmin, &vmax) : load_range(&ymin, &ymax);
  931.     if (!equals(c_token, "]"))
  932.         int_error("']' expected", c_token);
  933.     c_token++;
  934.     if (changed)
  935.         if(parametric) 
  936.             autoscale_lv = FALSE;
  937.         else
  938.             autoscale_ly = FALSE;
  939.     }
  940.     if (equals(c_token, "[")) {    /* set optional x (parametric) or z ranges */
  941.     c_token++;
  942.     changed = parametric ? load_range(&xmin, &xmax) : load_range(&zmin, &zmax);
  943.     if (!equals(c_token, "]"))
  944.         int_error("']' expected", c_token);
  945.     c_token++;
  946.     if (changed)
  947.         if(parametric) 
  948.             autoscale_lx = FALSE;
  949.         else
  950.             autoscale_lz = FALSE;
  951.     }
  952.     if (equals(c_token, "[")) {    /* set optional y ranges */
  953.     c_token++;
  954.     changed = load_range(&ymin, &ymax);
  955.     if (!equals(c_token, "]"))
  956.         int_error("']' expected", c_token);
  957.     c_token++;
  958.     if (changed)
  959.         autoscale_ly = FALSE;
  960.     }
  961.     if (equals(c_token, "[")) {    /* set optional z ranges */
  962.     c_token++;
  963.     changed = load_range(&zmin, &zmax);
  964.     if (!equals(c_token, "]"))
  965.         int_error("']' expected", c_token);
  966.     c_token++;
  967.     if (changed)
  968.         autoscale_lz = FALSE;
  969.     }
  970.     /* use the default dummy variable unless changed */
  971.     if (dummy_token0 >= 0)
  972.     copy_str(c_dummy_var[0], dummy_token0);
  973.     else
  974.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  975.  
  976.     if (dummy_token1 >= 0)
  977.     copy_str(c_dummy_var[1], dummy_token1);
  978.     else
  979.     (void) strcpy(c_dummy_var[1], dummy_var[1]);
  980.  
  981.     eval_3dplots();
  982. }
  983.  
  984.  
  985. define()
  986. {
  987.     register int    start_token;/* the 1st token in the function definition */
  988.     register struct udvt_entry *udv;
  989.     register struct udft_entry *udf;
  990.  
  991.     if (equals(c_token + 1, "(")) {
  992.     /* function ! */
  993.     int             dummy_num = 0;
  994.     start_token = c_token;
  995.     do {
  996.         c_token += 2;    /* skip to the next dummy */
  997.         copy_str(c_dummy_var[dummy_num++], c_token);
  998.     } while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR));
  999.     if (equals(c_token + 1, ","))
  1000.         int_error("function contains too many parameters", c_token + 2);
  1001.     c_token += 3;        /* skip (, dummy, ) and = */
  1002.     if (END_OF_COMMAND)
  1003.         int_error("function definition expected", c_token);
  1004.     udf = dummy_func = add_udf(start_token);
  1005.     if (udf->at)        /* already a dynamic a.t. there */
  1006.         free((char *) udf->at);    /* so free it first */
  1007.     if ((udf->at = perm_at()) == (struct at_type *) NULL)
  1008.         int_error("not enough memory for function", start_token);
  1009.     m_capture(&(udf->definition), start_token, c_token - 1);
  1010.     } else {
  1011.     /* variable ! */
  1012.     start_token = c_token;
  1013.     c_token += 2;
  1014.     udv = add_udv(start_token);
  1015.     (void) const_express(&(udv->udv_value));
  1016.     udv->udv_undef = FALSE;
  1017.     }
  1018. }
  1019.  
  1020. get_data(this_plot)
  1021.     struct curve_points *this_plot;
  1022. {
  1023.     register int    i, j, l_num, datum;
  1024.     int fcol[5], scol[5], ncol[5], prevmin, col;
  1025.     char  format[MAX_LINE_LEN + 1], data_file[MAX_LINE_LEN + 1],
  1026.           line[MAX_LINE_LEN + 1];
  1027.     /* conversion variables */
  1028.     int n, m, linestat, using;
  1029.     char *s;
  1030.     double val[5], v[5];
  1031.     float fval[5];    /* for use in sscanf */
  1032.  
  1033.     /* close forgotten input file (in case of a syntax error) */
  1034.     if( data_fp ) {
  1035. #if defined(unix) || defined(PIPES)
  1036.         if (pipe_open) {
  1037.             (void) pclose(data_fp);
  1038.             pipe_open = FALSE;
  1039.         } else
  1040. #endif /* unix || PIPES */
  1041.         (void) fclose(data_fp);
  1042.         data_fp=NULL;
  1043.     }
  1044.  
  1045.     quotel_str(data_file, c_token);
  1046.     this_plot->plot_type = DATA;
  1047. /*    if (parametric)
  1048.         int_error("Parametric data files not yet implemented", NO_CARET);
  1049. */
  1050. #if defined(unix) || defined(PIPES)
  1051.     if (*data_file == '<') {
  1052.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1053.             os_error("cannot create pipe for data", c_token);
  1054.         else
  1055.             pipe_open = TRUE;
  1056.     } else
  1057. #endif /* unix || PIPES */
  1058.     if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1059.         os_error("can't open data file", c_token);
  1060.  
  1061.     format[0] = '\0';
  1062.     for (i=0; i<5; i++)
  1063.         fcol[i] = i+1;
  1064.  
  1065.     using = 0;
  1066.     c_token++;            /* skip data file name */
  1067.  
  1068.     /* jev -- support for passing data from file thru user function */
  1069.     if (almost_equals(c_token, "thru$")) {
  1070.         c_token++;
  1071.         if (ydata_func.at)
  1072.             free(ydata_func.at);
  1073.         dummy_func = &ydata_func;
  1074.         ydata_func.at = perm_at();
  1075.     } else {
  1076.         if (ydata_func.at)
  1077.             free(ydata_func.at);
  1078.         ydata_func.at = NULL;
  1079.     }
  1080.  
  1081.     if (almost_equals(c_token,"u$sing")) {
  1082.         using = 1;
  1083.         c_token++;      /* skip "using" */
  1084.             
  1085.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1086.             struct value a;
  1087.             for (i=0; i<5; i++)
  1088.                 fcol[i] = -1;
  1089.             fcol[0] = (int)magnitude(const_express(&a));
  1090.             for (i=1; equals(c_token,":") && i<5; i++) {
  1091.                 c_token++;      /* skip ":" */
  1092.                 fcol[i] = (int)magnitude(const_express(&a));
  1093.             }
  1094.         }
  1095.  
  1096.         if (!END_OF_COMMAND && isstring(c_token)) {
  1097.             quotel_str(format, c_token);
  1098.             c_token++;    /* skip format */
  1099.         }
  1100.     }
  1101.     
  1102.     /* sort fcol[] into scol[] removing duplicates */
  1103.     prevmin = 0;
  1104.     for (i=0; i<5; i++) {
  1105.         col = 10000;
  1106.         for (j=0; j<5; j++)
  1107.             if ((fcol[j]>prevmin) && (fcol[j]<col))
  1108.                 col = fcol[j];
  1109.         if (col <10000)
  1110.                prevmin = scol[i] = col;
  1111.         else
  1112.             scol[i] = 0;
  1113.     }
  1114.     /* normalise fcol[] into ncol[] */
  1115.     for (i=0; i<5; i++) {
  1116.         if (fcol[i] > 0)
  1117.             for (j=0; j<5; j++) {
  1118.                 if (fcol[i] == scol[j])
  1119.                     ncol[i] = j+1;
  1120.             }
  1121.         else if (fcol[i] == 0)
  1122.             ncol[i] = 0;
  1123.         else
  1124.             ncol[i] = -1;
  1125.     }
  1126.     /* set col to highest column number */
  1127.     col = 0;
  1128.     for (i=0; i<5; i++) 
  1129.         if (fcol[i]>col) col=fcol[i];
  1130.  
  1131.     l_num = 0;
  1132.     datum = 0;
  1133.     i = 0;
  1134.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1135.         l_num++;
  1136.         if (is_comment(line[0]))
  1137.             continue;        /* ignore comments */
  1138.         if (i >= this_plot->p_max) {
  1139.             /*
  1140.              * overflow about to occur. Extend size of points[] array. We
  1141.              * either double the size, or add 1000 points, whichever is a
  1142.              * smaller increment. Note i=p_max.
  1143.              */
  1144.             cp_extend(this_plot, i + (i < 1000 ? i : 1000));
  1145.         }
  1146.         if (!line[1]) {        /* is it blank line ? */
  1147.             /* break in data, make next point undefined */
  1148.             this_plot->points[i].type = UNDEFINED;
  1149.             i++;
  1150.             continue;
  1151.         }
  1152.         if (strlen(format) != 0) {
  1153.             /* use old sscanf if a format string was given */
  1154.             m = sscanf(line, format, &fval[0], &fval[1], &fval[2], &fval[3], &fval[4]);
  1155.             for (n=0; n<5; n++)        /* convert floats from sscanf to double */
  1156.                 val[n] = (double)fval[n];
  1157.  
  1158.             for (j=0; j<5 && fcol[j]>=0 && fcol[j]-1<m ; j++)
  1159.                 if (fcol[j])
  1160.                     v[j] = val[fcol[j]-1];
  1161.                 else
  1162.                     v[j] = datum;    /* using 0:n */
  1163.         }
  1164.         else {
  1165.             /* implement our own sscanf that skips lines with invalid data 
  1166.              * if a using statement was given */
  1167.             /* convert the array to its constituents */
  1168.             for(n=0; n<5; n++)            /* wipe the array */
  1169.                 val[n] = 0.0;
  1170.             n=0;                        /* n is column number */
  1171.             m=0;                        /* m is number of values read */
  1172.             linestat = 1;                /* linestat: 1 OK 2 bad value 0 EOL */
  1173.             s = line;
  1174.             while ((linestat == 1) && (n<col)) {
  1175.                 while (isspace(*s)) s++;
  1176.                 if (*s == '\0') {
  1177.                     linestat = 0;
  1178.                     break;
  1179.                 }
  1180.                 n++;
  1181.                 if (n == scol[m]) {
  1182.                     if (isdigit(*s) || *s=='-' || *s=='+' || *s=='.') {
  1183.                         val[m] = atof(s);
  1184.                         m++;
  1185.                     }
  1186.                     else
  1187.                         linestat = 2;    /* abort the line non-digit in req loc */
  1188.                 }
  1189.                 while ((!isspace(*s)) && (*s != '\0')) s++;
  1190.             }
  1191.  
  1192.             if (using && (linestat == 2))
  1193.                 continue;    /* skip lines where a using pattern is present and not met */
  1194.  
  1195.             for (j=0; j<5 && ncol[j]>=0 && ncol[j]-1<m ; j++)
  1196.                 if (ncol[j])
  1197.                     v[j] = val[ncol[j]-1];
  1198.                 else
  1199.                     v[j] = datum;    /* using 0:n */
  1200.         }
  1201.  
  1202.         switch (j) {
  1203.             case 1: {        /* only one number */
  1204.                 /* x is index, assign number to y */
  1205.                 v[1]=v[0];
  1206.                 v[0]=datum;
  1207.                 /* nobreak */
  1208.             }
  1209.             case 2: {        /* x, y */
  1210.                 /* ylow and yhigh are same as y */
  1211.                 datum++;
  1212.                 store2d_point(this_plot, i++, v[0], v[1], v[1], v[1], -1.0);
  1213.                 break;
  1214.             }
  1215.             case 3: {        /* x, y, ydelta */
  1216.                 /* ydelta is in the ylow variable */
  1217.                 datum++;
  1218.                 store2d_point(this_plot, i++, v[0], v[1], v[1]-v[2], v[1]+v[2], -1.0);
  1219.                 break;
  1220.             }
  1221.             case 4: {        /* x, y, ylow, yhigh */
  1222.                 datum++;
  1223.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], -1.0);
  1224.                 break;
  1225.             }
  1226.             case 5: {        /* x, y, ylow, yhigh, width */
  1227.                 datum++;
  1228.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], v[4]);
  1229.                 break;
  1230.             }
  1231.             default: {
  1232.                 (void) sprintf(line, "bad data on line %d", l_num);
  1233.                 /* close file before exiting to command level */
  1234. #if defined(unix) || defined(PIPES)
  1235.                 if (pipe_open) {
  1236.                     (void) pclose(data_fp);
  1237.                     pipe_open = FALSE;
  1238.                 } else
  1239. #endif /* unix || PIPES */
  1240.                     (void) fclose(data_fp);
  1241.                 data_fp=NULL;
  1242.                 int_error(line, c_token);
  1243.             }
  1244.         }
  1245.     }
  1246.     this_plot->p_count = i;
  1247.     cp_extend(this_plot, i);    /* shrink to fit */
  1248.  
  1249. #if defined(unix) || defined(PIPES)
  1250.     if (pipe_open) {
  1251.         (void) pclose(data_fp);
  1252.         pipe_open = FALSE;
  1253.     } else
  1254. #endif /* unix || PIPES */
  1255.     (void) fclose(data_fp);
  1256.     data_fp=NULL;
  1257. }
  1258.  
  1259.  
  1260. /* called by get_data for each point */
  1261. store2d_point(this_plot, i, x, y, ylow, yhigh, width)
  1262.     struct curve_points *this_plot;
  1263.     int             i;        /* point number */
  1264.     double          x, y;
  1265.     double          ylow, yhigh;
  1266.     double          width;
  1267. {
  1268.     struct coordinate GPHUGE *cp = &(this_plot->points[i]);
  1269.  
  1270.     /* the easy part: */
  1271.     cp->type = INRANGE;
  1272.     cp->x = x;
  1273.     cp->y = y;
  1274.     cp->ylow = ylow;
  1275.     cp->yhigh = yhigh;
  1276.     cp->z = width;
  1277.  
  1278.     /* jev -- pass data values thru user-defined function */
  1279.     if (ydata_func.at) {
  1280.     struct value    val;
  1281.  
  1282.     (void) Gcomplex(&ydata_func.dummy_values[0], y, 0.0);
  1283.     evaluate_at(ydata_func.at, &val);
  1284.     cp->y = real(&val);
  1285.  
  1286.     (void) Gcomplex(&ydata_func.dummy_values[0], ylow, 0.0);
  1287.     evaluate_at(ydata_func.at, &val);
  1288.     cp->ylow = real(&val);
  1289.  
  1290.     (void) Gcomplex(&ydata_func.dummy_values[0], yhigh, 0.0);
  1291.     evaluate_at(ydata_func.at, &val);
  1292.     cp->yhigh = real(&val);
  1293.     }
  1294.     /* Adjust for log scale. */
  1295.     if (is_log_x) {
  1296.         cp->type = adjustlog(cp->type, &(cp->x), log_base_log_x);
  1297.         (void) adjustlog(cp->type, &(cp->z), log_base_log_z);
  1298.     }
  1299.     if (is_log_y) {
  1300.     cp->type = adjustlog(cp->type, &(cp->y), log_base_log_y);
  1301.     /* Note ylow,yhigh can't affect cp->type. */
  1302.     (void) adjustlog(cp->type, &(cp->ylow), log_base_log_y);
  1303.     (void) adjustlog(cp->type, &(cp->yhigh), log_base_log_y);
  1304.     }
  1305.     /* Now adjust the xrange, or declare the point out of range */
  1306.     /*
  1307.      * The yrange is handled later, once we know whether to include ylow,
  1308.      * yhigh in the calculation. See adjust_yrange()
  1309.      */
  1310.     if (cp->type == INRANGE)
  1311.     if (autoscale_lx || inrange(x, xmin, xmax)) {
  1312.         if (autoscale_lx) {
  1313.         if (x < xmin)
  1314.             xmin = x;
  1315.         if (x > xmax)
  1316.             xmax = x;
  1317.         }
  1318.     } else {
  1319.         cp->type = OUTRANGE;
  1320.     }
  1321. }
  1322.  
  1323.  
  1324. /*
  1325.  * Adjust for log scale: take the log of the second parameter, in place, if
  1326.  * possible. If not possible, return new type for point; if possible, then
  1327.  * return old type for point.  The log is taken to the base implicit in the
  1328.  * third parameter.
  1329.  */
  1330. static enum coord_type
  1331. adjustlog(type, val, log_base_log)
  1332.     enum coord_type type;
  1333.     coordval       *val;
  1334.     double          log_base_log;
  1335. {
  1336.     if (*val < 0.0) {
  1337.     return (UNDEFINED);
  1338.     } else if (*val == 0.0) {
  1339.     *val = -VERYLARGE;
  1340.     return (OUTRANGE);
  1341.     } else {
  1342.     *val = log(*val)/log_base_log;
  1343.     return (type);
  1344.     }
  1345. }
  1346.  
  1347.  
  1348. /* now adjust the yrange, or declare the point out of range */
  1349. /* this does all points in a curve */
  1350. adjust_yrange(curve)
  1351.     struct curve_points *curve;
  1352. {
  1353.     TBOOLEAN         ebars = (curve->plot_style == ERRORBARS);
  1354.     int             npoints = curve->p_count;    /* number of points */
  1355.     coordval        y, ylow, yhigh;    /* one point value */
  1356.     struct coordinate GPHUGE *cp;    /* one coordinate */
  1357.     int             i;        /* index into points */
  1358.  
  1359.     for (i = 0; i < npoints; i++) {
  1360.     cp = &(curve->points[i]);
  1361.     if (cp->type == INRANGE) {
  1362.         y = is_log_y ? pow(base_log_y, cp->y) : cp->y;
  1363.         if ((autoscale_ly ||
  1364.         /*
  1365.          * inrange((is_log_y ? pow(base_log_y, y) : y), ymin, ymax) ||
  1366.          */
  1367.          inrange((y), ymin, ymax) ||
  1368.          polar)) {
  1369.         if (autoscale_ly) {
  1370.             if (y < ymin)
  1371.             ymin = y;
  1372.             if (y > ymax)
  1373.             ymax = y;
  1374.             if (ebars) {
  1375.             ylow = is_log_y ? pow(base_log_y, cp->ylow) : cp->ylow;
  1376.             yhigh = is_log_y ? pow(base_log_y, cp->yhigh) : cp->yhigh;
  1377.             if (ylow < ymin)
  1378.                 ymin = ylow;
  1379.             if (ylow > ymax)
  1380.                 ymax = ylow;
  1381.             if (yhigh < ymin)
  1382.                 ymin = yhigh;
  1383.             if (yhigh > ymax)
  1384.                 ymax = yhigh;
  1385.             }
  1386.         }
  1387.         } else {
  1388.         cp->type = OUTRANGE;
  1389.         }
  1390.     }
  1391.     }
  1392. }
  1393.  
  1394. grid_nongrid_data(this_plot)
  1395. struct surface_points *this_plot;
  1396. {
  1397.     int i, j, k;
  1398.     double x, y, z, w, dx, dy, xmin, xmax, ymin, ymax;
  1399.     struct iso_curve *old_iso_crvs = this_plot->iso_crvs;
  1400.     struct iso_curve *icrv, *oicrv, *oicrvs;
  1401.  
  1402.     /* Compute XY bounding box on the original data. */
  1403.     xmin = xmax = old_iso_crvs->points[0].x;
  1404.     ymin = ymax = old_iso_crvs->points[0].y;
  1405.     for (icrv = old_iso_crvs; icrv != NULL; icrv = icrv->next) {
  1406.     struct coordinate GPHUGE *points = icrv->points;
  1407.  
  1408.     for (i = 0; i < icrv->p_count; i++, points++) {
  1409.         if (xmin > points->x)
  1410.         xmin = points->x;
  1411.         if (xmax < points->x)
  1412.         xmax = points->x;
  1413.         if (ymin > points->y)
  1414.         ymin = points->y;
  1415.         if (ymax < points->y)
  1416.         ymax = points->y;
  1417.     }
  1418.     }
  1419.  
  1420.     dx = (xmax - xmin) / (dgrid3d_row_fineness - 1);
  1421.     dy = (ymax - ymin) / (dgrid3d_row_fineness - 1);
  1422.  
  1423.     /* Create the new grid structure, and compute the low pass filtering from
  1424.      * non grid to grid structure.
  1425.      */
  1426.     this_plot->iso_crvs = NULL;
  1427.     this_plot->num_iso_read = dgrid3d_col_fineness;
  1428.     this_plot->has_grid_topology = TRUE;
  1429.     for (i = 0, x = xmin; i < dgrid3d_col_fineness; i++, x += dx) {
  1430.     struct coordinate GPHUGE *points;
  1431.  
  1432.     icrv = iso_alloc(dgrid3d_row_fineness + 1);
  1433.     icrv->p_count = dgrid3d_row_fineness;
  1434.     icrv->next = this_plot->iso_crvs;
  1435.     this_plot->iso_crvs = icrv;
  1436.     points = icrv->points;
  1437.  
  1438.     for (j = 0, y = ymin; j < dgrid3d_row_fineness; j++, y += dy, points++) {
  1439.         z = w = 0.0;
  1440.  
  1441.         for (oicrv = old_iso_crvs; oicrv != NULL; oicrv = oicrv->next) {
  1442.         struct coordinate GPHUGE *opoints = oicrv->points;
  1443.         for (k = 0; k < oicrv->p_count; k++, opoints++) {
  1444.             double dist,
  1445.                dist_x = fabs( opoints->x - x ),
  1446.                dist_y = fabs( opoints->y - y );
  1447.  
  1448.             switch (dgrid3d_norm_value) {
  1449.             case 1:
  1450.                 dist = dist_x + dist_y;
  1451.                 break;
  1452.             case 2:
  1453.                 dist = dist_x * dist_x + dist_y * dist_y;
  1454.                 break;
  1455.             case 4:
  1456.                 dist = dist_x * dist_x + dist_y * dist_y;
  1457.                 dist *= dist;
  1458.                 break;
  1459.             case 8:
  1460.                 dist = dist_x * dist_x + dist_y * dist_y;
  1461.                 dist *= dist;
  1462.                 dist *= dist;
  1463.                 break;
  1464.             case 16:
  1465.                 dist = dist_x * dist_x + dist_y * dist_y;
  1466.                 dist *= dist;
  1467.                 dist *= dist;
  1468.                 dist *= dist;
  1469.                 break;
  1470.             default:
  1471.                 dist = pow( dist_x, dgrid3d_norm_value ) +
  1472.                    pow( dist_y, dgrid3d_norm_value );
  1473.                 break;
  1474.             }
  1475.  
  1476.             /* The weight of this point is inverse proportional
  1477.              * to the distance.
  1478.              */
  1479.             if ( dist == 0.0 )
  1480. #ifndef AMIGA_SC_6_1
  1481.             dist = VERYLARGE;
  1482. #else /* AMIGA_SC_6_1 */
  1483.             /* Multiplying VERYLARGE by opoints->z below
  1484.              * might yield Inf (i.e. a number that can't
  1485.              * be represented on the machine). This will
  1486.              * result in points->z being set to NaN. It's
  1487.              * better to have a pretty large number that is
  1488.              * also on the safe side... The numbers that are
  1489.              * read by gnuplot are float values anyway, so
  1490.              * they can't be bigger than FLT_MAX. So setting
  1491.              * dist to FLT_MAX^2 will make dist pretty large
  1492.              * with respect to any value that has been read. */
  1493.             dist = ((double)FLT_MAX)*((double)FLT_MAX);
  1494. #endif /* AMIGA_SC_6_1 */
  1495.             else
  1496.             dist = 1.0 / dist;
  1497.  
  1498.             z += opoints->z * dist;
  1499.             w += dist;
  1500.         }
  1501.         }
  1502.  
  1503.         points->x = x;
  1504.         points->y = y;
  1505.         points->z = z / w;
  1506.         points->type = INRANGE;
  1507.     }
  1508.     }
  1509.     
  1510.     /* Delete the old non grid data. */
  1511.     for (oicrvs = old_iso_crvs; oicrvs != NULL;) {
  1512.     oicrv = oicrvs;
  1513.     oicrvs = oicrvs->next;
  1514.     iso_free(oicrv);
  1515.     }
  1516. }
  1517.  
  1518. get_3ddata(this_plot)
  1519.     struct surface_points *this_plot;
  1520. {
  1521.     register int    i, j, l_num, xdatum, ydatum;
  1522.     float           x, y, z;
  1523.     char            data_file[MAX_LINE_LEN + 1], line[MAX_LINE_LEN + 1];
  1524.     char           *float_format = "%f", *float_skip = "%*f";
  1525.     static TBOOLEAN  only_z = FALSE, using_format = FALSE;
  1526.     static int      xcol = 1, ycol = 2, zcol = 3, index = -1;
  1527.     static char     format[MAX_LINE_LEN + 1];
  1528.     int            pt_in_iso_crv = 0, maxcol, num_col;
  1529.     enum XYZ_order_type {
  1530.     XYZ, YXZ, ZXY, XZY, ZYX, YZX, XY, YX
  1531.     }               xyz_order;
  1532.     struct iso_curve *this_iso;
  1533.  
  1534.     /* close forgotten file (in case of a syntax error) */
  1535.     if (data_fp && !more_data_fp) {
  1536. #if defined(unix) || defined(PIPES)
  1537.     if (pipe_open) {
  1538.         (void) pclose(data_fp);
  1539.         pipe_open = FALSE;
  1540.     } else
  1541. #endif /* unix || PIPES */
  1542.         (void) fclose(data_fp);
  1543.         data_fp=NULL;
  1544.         }
  1545.  
  1546.     quotel_str(data_file, c_token);
  1547.     this_plot->plot_type = DATA3D;
  1548.     this_plot->has_grid_topology = TRUE;
  1549.     if (!more_data_fp) {
  1550. #if defined(unix) || defined(PIPES)
  1551.     if (*data_file == '<') {
  1552.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1553.         os_error("cannot create pipe; output not changed", c_token);
  1554.         else
  1555.         pipe_open = TRUE;
  1556.     } else
  1557. #endif /* unix || PIPES */
  1558.         if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1559.         os_error("can't open data file", c_token);
  1560.  
  1561.     /* Initialize defualt values. */
  1562.     only_z = FALSE;
  1563.     using_format = FALSE;
  1564.     xcol = 1;
  1565.     ycol = 2;
  1566.     zcol = 3;
  1567.     index = -1;
  1568.     format[0] = '\0';
  1569.  
  1570.     c_token++;            /* skip data file name */
  1571.     if (almost_equals(c_token, "i$ndex")) {
  1572.         struct value a;
  1573.         c_token++;        /* skip "index" */
  1574.         index = (int) magnitude(const_express(&a));
  1575.     }
  1576.     if (almost_equals(c_token, "u$sing")) {
  1577.         c_token++;        /* skip "using" */
  1578.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1579.         struct value a;
  1580.         zcol = (int) magnitude(const_express(&a));
  1581.         only_z = TRUE;
  1582.         if (equals(c_token, ":")) {
  1583.             c_token++;    /* skip ":" */
  1584.             only_z = FALSE;
  1585.             ycol = zcol;
  1586.             zcol = (int) magnitude(const_express(&a));
  1587.             if (equals(c_token, ":")) {
  1588.             c_token++;    /* skip ":" */
  1589.             xcol = ycol;
  1590.             ycol = zcol;
  1591.             zcol = (int) magnitude(const_express(&a));
  1592.             } else {
  1593.             if (mapping3d == MAP3D_CARTESIAN)
  1594.                 int_error("Must specify 1 or 3 columns", c_token);
  1595.             xcol = ycol;
  1596.             ycol = zcol;
  1597.             }
  1598.         }
  1599.         if (!only_z)
  1600.             if ((xcol == ycol) || (ycol == zcol) || (xcol == zcol))
  1601.             int_error("Columns must be distinct", c_token);
  1602.         }
  1603.         if (!END_OF_COMMAND && isstring(c_token)) {
  1604.         quotel_str(format, c_token);
  1605.         using_format = TRUE;
  1606.         c_token++;        /* skip format */
  1607.         }
  1608.     } else {
  1609.         if ( (only_z = !parametric) != FALSE)
  1610.         zcol = 1;
  1611.     }
  1612.     }
  1613.  
  1614.     switch (mapping3d) {
  1615.     case MAP3D_CARTESIAN:
  1616.     maxcol = (xcol > ycol) ? xcol : ycol;
  1617.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1618.     if (!only_z) {        /* Determine ordering of input columns */
  1619.         if (zcol == maxcol) {
  1620.         if (xcol < ycol)
  1621.             xyz_order = XYZ;    /* scanf(x,y,z) */
  1622.         else
  1623.             xyz_order = YXZ;    /* scanf(y,x,z) */
  1624.         } else if (ycol == maxcol) {
  1625.         if (xcol < zcol)
  1626.             xyz_order = XZY;    /* scanf(x,z,y) */
  1627.         else
  1628.             xyz_order = ZXY;    /* scanf(z,x,y) */
  1629.         } else {
  1630.         if (ycol < zcol)
  1631.             xyz_order = YZX;    /* scanf(y,z,x) */
  1632.         else
  1633.             xyz_order = ZYX;    /* scanf(z,y,x) */
  1634.         }
  1635.     }
  1636.     if (strlen(format) == 0) {
  1637.         if (only_z) {
  1638.         for (i = 1; i <= zcol; i++)
  1639.             if (i == zcol)
  1640.             (void) strcat(format, float_format);
  1641.             else
  1642.             (void) strcat(format, float_skip);
  1643.         } else {
  1644.         for (i = 1; i <= maxcol; i++)
  1645.             if ((i == xcol) || (i == ycol) || (i == zcol))
  1646.             (void) strcat(format, float_format);
  1647.             else
  1648.             (void) strcat(format, float_skip);
  1649.         }
  1650.     }
  1651.     break;
  1652.     case MAP3D_SPHERICAL:
  1653.     case MAP3D_CYLINDRICAL:
  1654.     if (only_z)
  1655.         int_error("Two or three columns for spherical/cylindrical coords.", c_token);
  1656.     maxcol = (xcol > ycol) ? xcol : ycol;
  1657.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1658.     xyz_order = (xcol < ycol) ? XY : YX;
  1659.     for (i = 1; i <= maxcol; i++)
  1660.         if ((i == xcol) || (i == ycol))
  1661.         (void) strcat(format, float_format);
  1662.         else
  1663.         (void) strcat(format, float_skip);
  1664.     }
  1665.  
  1666.     l_num = 0;
  1667.     xdatum = 0;
  1668.     ydatum = 0;
  1669.     this_plot->num_iso_read = 0;
  1670.     this_plot->has_grid_topology = TRUE;
  1671.     if (this_plot->iso_crvs != NULL) {
  1672.     struct iso_curve *icrv, *icrvs = this_plot->iso_crvs;
  1673.  
  1674.     while (icrvs) {
  1675.         icrv = icrvs;
  1676.         icrvs = icrvs->next;
  1677.         iso_free(icrv);
  1678.     }
  1679.     this_plot->iso_crvs = NULL;
  1680.     }
  1681.     if (!more_data_fp && is_binary_file(data_fp)) {    /* MOD--RKC */
  1682. #if defined(MSDOS)||defined(ATARI)||defined(OS2)||defined(_Windows)||defined(DOS386)
  1683.     /* file must be opened with binary flag. the old cr/lf problem again */
  1684. #ifdef PIPES
  1685.     if( pipe_open ) {
  1686.       pclose(data_fp);
  1687.       data_fp=NULL;
  1688.       pipe_open=FALSE;
  1689.       int_error("binary data from pipes is not implemented", NO_CARET);
  1690.     }
  1691. #endif
  1692.     data_fp = freopen(data_file, "rb", data_fp);
  1693. #endif
  1694.     xdatum = get_binary_data(this_plot, data_fp, &this_iso);
  1695.     } else {
  1696.     int last_line_blank = FALSE;
  1697.  
  1698.     more_data_fp = FALSE;
  1699.  
  1700.     this_iso = iso_alloc(samples);
  1701.  
  1702.     if (index > 0) { /* Skip data meshes until mesh index is reached. */
  1703.         int i = index;
  1704.  
  1705.         while (i--) {
  1706.         while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1707.             l_num++;
  1708.  
  1709.             if (!line[1]) {    /* is it blank line ? */
  1710.             if (last_line_blank) /* Two consecutive blanks. */
  1711.                 break;
  1712.             last_line_blank = TRUE;
  1713.             }
  1714.             else
  1715.             last_line_blank = FALSE;
  1716.         }
  1717.         if (feof(data_fp))
  1718.             int_error("mesh index overflow", NO_CARET);
  1719.         }
  1720.     }   /* end of index skip */
  1721.     
  1722.     last_line_blank = FALSE;
  1723.  
  1724.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1725.         l_num++;
  1726.         if (is_comment(line[0]))
  1727.         continue;    /* ignore comments */
  1728.         if (!line[1]) {    /* is it blank line ? */
  1729.         if (last_line_blank) { /* Two consecutive blank lines. */
  1730.             more_data_fp = TRUE;
  1731.             break;
  1732.         }
  1733.         last_line_blank = TRUE;
  1734.  
  1735.         if (pt_in_iso_crv == 0) {
  1736.             if (xdatum == 0)
  1737.             continue;
  1738.             pt_in_iso_crv = xdatum;
  1739.         }
  1740.         if (xdatum > 0) {
  1741.             this_iso->p_count = xdatum;
  1742.             this_iso->next = this_plot->iso_crvs;
  1743.             this_plot->iso_crvs = this_iso;
  1744.             this_plot->num_iso_read++;
  1745.  
  1746.             if (xdatum != pt_in_iso_crv)
  1747.             this_plot->has_grid_topology = FALSE;
  1748.  
  1749.             this_iso = iso_alloc(pt_in_iso_crv);
  1750.             xdatum = 0;
  1751.             ydatum++;
  1752.         }
  1753.         continue;
  1754.         }
  1755.         last_line_blank = FALSE;
  1756.         if (xdatum >= this_iso->p_max) {
  1757.         /*
  1758.          * overflow about to occur. Extend size of points[] array. We
  1759.          * either double the size, or add 1000 points, whichever is a
  1760.          * smaller increment. Note i=p_max.
  1761.          */
  1762.         iso_extend(this_iso,
  1763.                xdatum + (xdatum < 1000 ? xdatum : 1000));
  1764.         }
  1765.         switch (num_col = sscanf(line, format, &x, &y, &z)) {
  1766.         case 3:        /* All parameter are specified. */
  1767.         if (!only_z || using_format) {
  1768.             switch (xyz_order) {
  1769.             case XYZ:    /* scanf(x,y,z) */
  1770.             this_iso->points[xdatum].x = x;
  1771.             this_iso->points[xdatum].y = y;
  1772.             this_iso->points[xdatum].z = z;
  1773.             break;
  1774.             case XZY:    /* scanf(x,z,y) */
  1775.             this_iso->points[xdatum].x = x;
  1776.             this_iso->points[xdatum].y = z;
  1777.             this_iso->points[xdatum].z = y;
  1778.             break;
  1779.             case YXZ:    /* scanf(y,x,z) */
  1780.             this_iso->points[xdatum].x = y;
  1781.             this_iso->points[xdatum].y = x;
  1782.             this_iso->points[xdatum].z = z;
  1783.             break;
  1784.             case ZXY:    /* scanf(z,x,y) */
  1785.             this_iso->points[xdatum].x = y;
  1786.             this_iso->points[xdatum].y = z;
  1787.             this_iso->points[xdatum].z = x;
  1788.             break;
  1789.             case YZX:    /* scanf(y,z,x) */
  1790.             this_iso->points[xdatum].x = z;
  1791.             this_iso->points[xdatum].y = x;
  1792.             this_iso->points[xdatum].z = y;
  1793.             break;
  1794.             case ZYX:    /* scanf(z,y,x) */
  1795.             this_iso->points[xdatum].x = z;
  1796.             this_iso->points[xdatum].y = y;
  1797.             this_iso->points[xdatum].z = x;
  1798.             break;
  1799.             }
  1800.             if (xyz_order != XYZ) {
  1801.             x = this_iso->points[xdatum].x;
  1802.             y = this_iso->points[xdatum].y;
  1803.             z = this_iso->points[xdatum].z;
  1804.             }
  1805.             if (!parametric)
  1806.             int_error("Must be in parametric mode.",
  1807.                   NO_CARET);
  1808.             break;
  1809.         }
  1810.         case 1:        /* only one number on the line */
  1811.         if (!only_z && !using_format)
  1812.             int_error("3 columns expected, only 1 found", c_token);
  1813.         /* assign that number to z */
  1814.         this_iso->points[xdatum].z = x;
  1815.         z = x;
  1816.         this_iso->points[xdatum].x = xdatum;
  1817.         x = this_iso->points[xdatum].x;
  1818.         this_iso->points[xdatum].y = ydatum;
  1819.         y = this_iso->points[xdatum].y;
  1820.         if (parametric)
  1821.             int_error("Must be in non parametric mode.",
  1822.                   NO_CARET);
  1823.         break;
  1824.         case 2:
  1825.         switch (xyz_order) {
  1826.         case YX:
  1827.             z = x;    /* Use z as temp */
  1828.             x = y;
  1829.             y = z;
  1830.             break;
  1831.         default:
  1832.             break;
  1833.         }
  1834.         switch (mapping3d) {
  1835.         case MAP3D_CARTESIAN:
  1836.             int_error("2 columns found, 1 or 3 expected",
  1837.                   c_token);
  1838.             break;
  1839.         case MAP3D_SPHERICAL:
  1840.             if (angles_format == ANGLES_DEGREES) {
  1841.             x *= DEG2RAD;    /* Convert to radians. */
  1842.             y *= DEG2RAD;
  1843.             }
  1844.             if( num_col == 2) z = 1.0;
  1845.             this_iso->points[xdatum].x = z*cos(x) * cos(y);
  1846.             this_iso->points[xdatum].y = z*sin(x) * cos(y);
  1847.             this_iso->points[xdatum].z = z*sin(y);
  1848.             break;
  1849.         case MAP3D_CYLINDRICAL:
  1850.             if (angles_format == ANGLES_DEGREES)
  1851.             x *= DEG2RAD;    /* Convert to radians. */
  1852.             if( num_col == 2) z = 1.0;
  1853.             this_iso->points[xdatum].x = z*cos(x);
  1854.             this_iso->points[xdatum].y = z*sin(x);
  1855.             this_iso->points[xdatum].z = y;
  1856.             break;
  1857.         }
  1858.         x = this_iso->points[xdatum].x;
  1859.         y = this_iso->points[xdatum].y;
  1860.         z = this_iso->points[xdatum].z;
  1861.         break;
  1862.         default:
  1863.         (void) sprintf(line, "bad data on line %d", l_num);
  1864.         int_error(line, c_token);
  1865.         }
  1866.  
  1867.         if (is_log_x) {
  1868.         if (x <= 0.0)
  1869.             int_error("X value must be above 0 for log scale!",
  1870.                   NO_CARET);
  1871.         else
  1872.             this_iso->points[xdatum].x =
  1873.             log(this_iso->points[xdatum].x)/log_base_log_x;
  1874.         }
  1875.         if (is_log_y) {
  1876.         if (y <= 0.0)
  1877.             int_error("Y value must be above 0 for log scale!",
  1878.                   NO_CARET);
  1879.         else
  1880.             this_iso->points[xdatum].y =
  1881.             log(this_iso->points[xdatum].y)/log_base_log_y;
  1882.         }
  1883.         if (is_log_z) {
  1884.         if (z <= 0.0)
  1885.             int_error("Z value must be above 0 for log scale!",
  1886.                   NO_CARET);
  1887.         else
  1888.             this_iso->points[xdatum].z =
  1889.             log(this_iso->points[xdatum].z)/log_base_log_z;
  1890.         }
  1891.         if (autoscale_lx) {
  1892.         if (x < xmin)
  1893.             xmin = x;
  1894.         if (x > xmax)
  1895.             xmax = x;
  1896.         }
  1897.         if (autoscale_ly) {
  1898.         if (y < ymin)
  1899.             ymin = y;
  1900.         if (y > ymax)
  1901.             ymax = y;
  1902.         }
  1903.         if (autoscale_lz) {
  1904.         if (z < zmin)
  1905.             zmin = z;
  1906.         if (z > zmax)
  1907.             zmax = z;
  1908.         }
  1909.         xdatum++;    
  1910.     }  /* end of while loop */
  1911.  
  1912.     if (xdatum > 0) {
  1913.         this_plot->num_iso_read++;    /* Update last iso. */
  1914.         this_iso->p_count = xdatum;
  1915.  
  1916.         this_iso->next = this_plot->iso_crvs;
  1917.         this_plot->iso_crvs = this_iso;
  1918.  
  1919.         if (xdatum != pt_in_iso_crv)
  1920.         this_plot->has_grid_topology = FALSE;
  1921.  
  1922.     } else {
  1923.         iso_free(this_iso);    /* Free last allocation. */
  1924.     }
  1925.     }                /* MOD-RKC else of binary */
  1926.  
  1927.     if (index >= 0) more_data_fp = FALSE; /* Only one data set please. */
  1928.  
  1929.     if (!more_data_fp) {
  1930.       if (this_plot->num_iso_read <= 1)
  1931.       this_plot->has_grid_topology = FALSE;
  1932. #if defined(unix) || defined(PIPES)
  1933.       if (pipe_open) {
  1934.     if (this_plot->has_grid_topology && !hidden3d) {
  1935.       (void) pclose(data_fp);
  1936.       pipe_open = FALSE;
  1937.     }
  1938.       } else 
  1939. #endif /* unix || PIPES */
  1940.       {
  1941.     (void) fclose(data_fp);  
  1942.     data_fp = NULL;
  1943.       }
  1944.     }
  1945.  
  1946.     if (dgrid3d) grid_nongrid_data(this_plot);
  1947.  
  1948.     if (this_plot->num_iso_read <= 1)
  1949.     this_plot->has_grid_topology = FALSE;
  1950.     if (this_plot->has_grid_topology && !hidden3d) {
  1951.     struct iso_curve *new_icrvs = NULL;
  1952.     int             num_new_iso = this_plot->iso_crvs->p_count, len_new_iso = this_plot->num_iso_read;
  1953.  
  1954.     /* Now we need to set the other direction (pseudo) isolines. */
  1955.     for (i = 0; i < num_new_iso; i++) {
  1956.         struct iso_curve *new_icrv = iso_alloc(len_new_iso);
  1957.  
  1958.         new_icrv->p_count = len_new_iso;
  1959.  
  1960.         for (j = 0, this_iso = this_plot->iso_crvs;
  1961.          this_iso != NULL;
  1962.          j++, this_iso = this_iso->next) {
  1963.         new_icrv->points[j].x = this_iso->points[i].x;
  1964.         new_icrv->points[j].y = this_iso->points[i].y;
  1965.         new_icrv->points[j].z = this_iso->points[i].z;
  1966.         }
  1967.  
  1968.         new_icrv->next = new_icrvs;
  1969.         new_icrvs = new_icrv;
  1970.     }
  1971.  
  1972.     /* Append the new iso curves after the read ones. */
  1973.     for (this_iso = this_plot->iso_crvs;
  1974.          this_iso->next != NULL;
  1975.          this_iso = this_iso->next);
  1976.         this_iso->next = new_icrvs;
  1977.     }
  1978. }
  1979.  
  1980. /*
  1981.  * print_points: a debugging routine to print out the points of a curve, and
  1982.  * the curve structure. If curve<0, then we print the list of curves.
  1983.  */
  1984.  
  1985. static char    *plot_type_names[4] =
  1986. {
  1987.     "Function", "Data", "3D Function", "3d data"
  1988. };
  1989. static char    *plot_style_names[6] =
  1990. {
  1991.     "Lines", "Points", "Impulses", "LinesPoints", "Dots", "Errorbars"
  1992. };
  1993.  
  1994. print_points(curve)
  1995.     int             curve;    /* which curve to print */
  1996. {
  1997.     register struct curve_points *this_plot;
  1998.     int             i;
  1999.  
  2000.     if (curve < 0) {
  2001.     for (this_plot = first_plot, i = 0;
  2002.          this_plot != NULL;
  2003.          i++, this_plot = this_plot->next_cp) {
  2004.         printf("Curve %d:\n", i);
  2005.         if ((int) this_plot->plot_type >= 0 && (int) (this_plot->plot_type) < 4)
  2006.         printf("Plot type %d: %s\n", (int) (this_plot->plot_type),
  2007.                plot_type_names[(int) (this_plot->plot_type)]);
  2008.         else
  2009.         printf("Plot type %d: BAD\n", (int) (this_plot->plot_type));
  2010.         if ((int) this_plot->plot_style >= 0 && (int) (this_plot->plot_style) < 6)
  2011.         printf("Plot style %d: %s\n", (int) (this_plot->plot_style),
  2012.                plot_style_names[(int) (this_plot->plot_style)]);
  2013.         else
  2014.         printf("Plot style %d: BAD\n", (int) (this_plot->plot_style));
  2015.         printf("Plot title: '%s'\n", this_plot->title);
  2016.         printf("Line type %d\n", this_plot->line_type);
  2017.         printf("Point type %d\n", this_plot->point_type);
  2018.         printf("max points %d\n", this_plot->p_max);
  2019.         printf("current points %d\n", this_plot->p_count);
  2020.         printf("\n");
  2021.     }
  2022.     } else {
  2023.     for (this_plot = first_plot, i = 0;
  2024.          i < curve && this_plot != NULL;
  2025.          i++, this_plot = this_plot->next_cp);
  2026.     if (this_plot == NULL)
  2027.         printf("Curve %d does not exist; list has %d curves\n", curve, i);
  2028.     else {
  2029.         printf("Curve %d, %d points\n", curve, this_plot->p_count);
  2030.         for (i = 0; i < this_plot->p_count; i++) {
  2031.         printf("%c x=%g y=%g z=%g ylow=%g yhigh=%g\n",
  2032.                this_plot->points[i].type == INRANGE ? 'i'
  2033.                : this_plot->points[i].type == OUTRANGE ? 'o'
  2034.                : 'u',
  2035.                this_plot->points[i].x,
  2036.                this_plot->points[i].y,
  2037.                this_plot->points[i].z,
  2038.                this_plot->points[i].ylow,
  2039.                this_plot->points[i].yhigh);
  2040.         }
  2041.         printf("\n");
  2042.     }
  2043.     }
  2044. }
  2045.  
  2046. print_table()
  2047. {
  2048.     register struct curve_points *this_plot;
  2049.     int             i, curve;
  2050.  
  2051.     for (this_plot = first_plot, curve = 0; this_plot != NULL;
  2052.      curve++, this_plot = this_plot->next_cp) {
  2053.     fprintf(outfile, "Curve %d, %d points\n", curve, this_plot->p_count);
  2054.     for (i = 0; i < this_plot->p_count; i++) {
  2055.         fprintf(outfile, "%c x=%g y=%g\n",
  2056.             this_plot->points[i].type == INRANGE ? 'i'
  2057.             : this_plot->points[i].type == OUTRANGE ? 'o'
  2058.             : 'u',
  2059.             this_plot->points[i].x,
  2060.             this_plot->points[i].y);
  2061.     }
  2062.     fprintf(outfile, "\n");
  2063.     }
  2064.     fflush(outfile);
  2065. }
  2066.  
  2067. print_3dtable(pcount)
  2068. int pcount;
  2069. {
  2070.     register struct surface_points *this_plot;
  2071.     int             i, curve,surface;
  2072.     struct iso_curve *icrvs;
  2073.     struct coordinate GPHUGE *points;
  2074.  
  2075.     for (surface=0, this_plot=first_3dplot ; surface < pcount; 
  2076.         this_plot=this_plot->next_sp, surface++){
  2077.         fprintf(outfile, "\nSurface %d of %d surfaces\n", surface, pcount);
  2078.         icrvs = this_plot->iso_crvs;
  2079.         curve = 0;
  2080.  
  2081.         while(icrvs){
  2082.             fprintf(outfile, "\nIsoCurve %d, %d points\n", curve, icrvs->p_count);
  2083.             for(i=0, points = icrvs->points; i < icrvs->p_count; i++){
  2084.                 fprintf(outfile, "%c x=%g y=%g z=%g\n",
  2085.                 points[i].type == INRANGE ? 'i'
  2086.                 : points[i].type == OUTRANGE ? 'o'
  2087.                 : 'u',
  2088.                 points[i].x,
  2089.                 points[i].y,
  2090.                 points[i].z);
  2091.             }
  2092.             icrvs = icrvs->next;
  2093.             curve++;
  2094.         }
  2095.     fprintf(outfile, "\n");
  2096.     }
  2097.     fflush(outfile);
  2098. }
  2099.  
  2100. /*
  2101.  * This parses the plot command after any range specifications. To support
  2102.  * autoscaling on the x axis, we want any data files to define the x range,
  2103.  * then to plot any functions using that range. We thus parse the input
  2104.  * twice, once to pick up the data files, and again to pick up the functions.
  2105.  * Definitions are processed twice, but that won't hurt.
  2106.  */
  2107. eval_plots()
  2108. {
  2109.     register int    i;
  2110.     register struct curve_points *this_plot, **tp_ptr;
  2111.     register int    start_token, end_token;
  2112.     register int    begin_token;
  2113.     double          x_min, x_max, y_min, y_max;
  2114.     register double x, xdiff, temp;
  2115.     static struct value a;
  2116.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2117.     int             plot_num, line_num, point_num, xparam = 0;
  2118.     char           *xtitle;
  2119.     void            parametric_fixup();
  2120.  
  2121.     /* Reset first_plot. This is usually done at the end of this function.
  2122.        If there is an error within this function, the memory is left allocated,
  2123.        since we cannot call cp_free if the list is incomplete. Making sure that
  2124.        the list structure is always vaild requires some rewriting */
  2125.     first_plot=NULL;
  2126.  
  2127.     if (autoscale_ly) {
  2128.     ymin = VERYLARGE;
  2129.     ymax = -VERYLARGE;
  2130.     } else if (is_log_y && (ymin <= 0.0 || ymax <= 0.0))
  2131.     int_error("y range must be above 0 for log scale!",
  2132.           NO_CARET);
  2133.  
  2134.     tp_ptr = &(first_plot);
  2135.     plot_num = 0;
  2136.     line_num = 0;        /* default line type */
  2137.     point_num = 0;        /* default point type */
  2138.  
  2139.     xtitle = NULL;
  2140.  
  2141.     begin_token = c_token;
  2142.  
  2143.     /*** First Pass: Read through data files ***
  2144.      * This pass serves to set the xrange and to parse the command, as well
  2145.      * as filling in every thing except the function data. That is done after
  2146.      * the xrange is defined.
  2147.      */
  2148.     while (TRUE) {
  2149.     if (END_OF_COMMAND)
  2150.         int_error("function to plot expected", c_token);
  2151.  
  2152.     start_token = c_token;
  2153.  
  2154.     if (is_definition(c_token)) {
  2155.         define();
  2156.     } else {
  2157.         plot_num++;
  2158.  
  2159.         if (isstring(c_token)) {    /* data file to plot */
  2160.         if (parametric && xparam)
  2161.             int_error("previous parametric function not fully specified",
  2162.                   c_token);
  2163.  
  2164.         if (!some_data_files && autoscale_lx) {
  2165.             xmin = VERYLARGE;
  2166.             xmax = -VERYLARGE;
  2167.         }
  2168.         some_data_files = TRUE;
  2169.  
  2170.         if (*tp_ptr)
  2171.             this_plot = *tp_ptr;
  2172.         else {        /* no memory malloc()'d there yet */
  2173.             this_plot = cp_alloc(MIN_CRV_POINTS);
  2174.             *tp_ptr = this_plot;
  2175.         }
  2176.         this_plot->plot_type = DATA;
  2177.         this_plot->plot_style = data_style;
  2178.         end_token = c_token;
  2179.         get_data(this_plot);    /* this also parses the using option */
  2180.         } else {        /* function to plot */
  2181.         if (parametric)    /* working on x parametric function */
  2182.             xparam = 1 - xparam;
  2183.         if (*tp_ptr) {
  2184.             this_plot = *tp_ptr;
  2185.             cp_extend(this_plot, samples + 1);
  2186.         } else {    /* no memory malloc()'d there yet */
  2187.             this_plot = cp_alloc(samples + 1);
  2188.             *tp_ptr = this_plot;
  2189.         }
  2190.         this_plot->plot_type = FUNC;
  2191.         this_plot->plot_style = func_style;
  2192.         dummy_func = &plot_func;
  2193.         plot_func.at = temp_at();
  2194.         /* ignore it for now */
  2195.         end_token = c_token - 1;
  2196.         }
  2197.  
  2198.         if (almost_equals(c_token, "t$itle")) {
  2199.         if (parametric) {
  2200.             if (xparam)
  2201.             int_error(
  2202.                      "\"title\" allowed only after parametric function fully specified",
  2203.                      c_token);
  2204.             else if (xtitle != NULL)
  2205.             xtitle[0] = '\0';    /* Remove default title . */
  2206.         }
  2207.         c_token++;
  2208.         if (isstring(c_token)) {
  2209.             m_quote_capture(&(this_plot->title), c_token, c_token);
  2210.         } else {
  2211.             int_error("expecting \"title\" for plot", c_token);
  2212.         }
  2213.         c_token++;
  2214.         } else if (almost_equals(c_token, "not$itle")) {
  2215.             c_token++;
  2216.         } else {
  2217.         m_capture(&(this_plot->title), start_token, end_token);
  2218.         if (xparam)
  2219.             xtitle = this_plot->title;
  2220.         }
  2221.  
  2222.         this_plot->line_type = line_num;
  2223.         this_plot->point_type = point_num;
  2224.  
  2225.         if (almost_equals(c_token, "w$ith")) {
  2226.         if (parametric && xparam)
  2227.             int_error("\"with\" allowed only after parametric function fully specified",
  2228.                   c_token);
  2229.         this_plot->plot_style = get_style();
  2230.         }
  2231.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2232.         struct value    t;
  2233.         this_plot->line_type = (int) real(const_express(&t)) - 1;
  2234.         }
  2235.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2236.         struct value    t;
  2237.         this_plot->point_type = (int) real(const_express(&t)) - 1;
  2238.         }
  2239.         if ((this_plot->plot_style == POINTSTYLE) ||
  2240.         (this_plot->plot_style == LINESPOINTS) ||
  2241.         (this_plot->plot_style == ERRORBARS))
  2242.         if (!xparam)
  2243.             point_num++;
  2244.         if (!xparam)
  2245.         line_num++;
  2246.  
  2247.         if (this_plot->plot_type == DATA)
  2248.         /* now that we know the plot style, adjust the yrange */
  2249.         adjust_yrange(this_plot);
  2250.  
  2251.         tp_ptr = &(this_plot->next_cp);
  2252.     }
  2253.  
  2254.     if (equals(c_token, ","))
  2255.         c_token++;
  2256.     else
  2257.         break;
  2258.     }
  2259.  
  2260.     if (parametric && xparam)
  2261.     int_error("parametric function not fully specified", NO_CARET);
  2262.  
  2263.     if (parametric) {
  2264.     /* Swap t and x ranges for duration of these eval_plot computations. */
  2265.     ltmp = autoscale_lx;
  2266.     autoscale_lx = autoscale_lt;
  2267.     autoscale_lt = ltmp;
  2268.     temp = xmin;
  2269.     xmin = tmin;
  2270.     tmin = temp;
  2271.     temp = xmax;
  2272.     xmax = tmax;
  2273.     tmax = temp;
  2274.     }
  2275.     /*** Second Pass: Evaluate the functions ***/
  2276.     /*
  2277.      * Everything is defined now, except the function data. We expect no
  2278.      * syntax errors, etc, since the above parsed it all. This makes the code
  2279.      * below simpler. If autoscale_ly, the yrange may still change.
  2280.      */
  2281.     if (fabs(xmax - xmin) < zero)
  2282.     if (autoscale_lx) {
  2283.         fprintf(stderr, "Warning: empty %c range [%g:%g], ",
  2284.             parametric ? 't' : 'x', xmin, xmax);
  2285.         if (fabs(xmin) < zero) {
  2286.         /* completely arbitary */
  2287.         xmin = -1.;
  2288.         xmax = 1.;
  2289.         } else {
  2290.         /* expand range by 10% in either direction */
  2291.         xmin = xmin * 0.9;
  2292.         xmax = xmax * 1.1;
  2293.         }
  2294.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2295.     } else {
  2296.         int_error("x range is less than `zero`", c_token);
  2297.     }
  2298.  
  2299.     /* give error if xrange badly set from missing datafile error */
  2300.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2301.     int_error("x range is invalid", c_token);
  2302.     }
  2303.     if (is_log_x) {
  2304.     if (xmin <= 0.0 || xmax <= 0.0)
  2305.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2306.     x_min = log(xmin)/log_base_log_x;
  2307.     x_max = log(xmax)/log_base_log_x;
  2308.     } else {
  2309.     x_min = xmin;
  2310.     x_max = xmax;
  2311.     }
  2312.  
  2313.     xdiff = (x_max - x_min) / (samples - 1);
  2314.  
  2315.     tp_ptr = &(first_plot);
  2316.     plot_num = 0;
  2317.     this_plot = first_plot;
  2318.     c_token = begin_token;    /* start over */
  2319.  
  2320.     /* Read through functions */
  2321.     while (TRUE) {
  2322.     if (is_definition(c_token)) {
  2323.         define();
  2324.     } else {
  2325.         plot_num++;
  2326.         if (isstring(c_token)) {    /* data file to plot */
  2327.         /* ignore this now */
  2328.         c_token++;
  2329.  
  2330.         /*
  2331.          * jev -- support for passing data from file thru user
  2332.          * function
  2333.          */
  2334.         if (almost_equals(c_token, "thru$")) {
  2335.             struct udft_entry tmp;
  2336.             c_token++;
  2337.             dummy_func = &tmp;
  2338.             (void) temp_at();
  2339.         }
  2340.         if (almost_equals(c_token, "u$sing")) {
  2341.             c_token++;    /* skip "using" */
  2342.             if (!isstring(c_token)) {
  2343.             struct value    a;
  2344.             (void) magnitude(const_express(&a));    /* skip xcol */
  2345.             if (equals(c_token, ":")) {
  2346.                 c_token++;    /* skip ":" */
  2347.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2348.             }
  2349.             if (equals(c_token, ":")) {
  2350.                 c_token++;    /* skip ":" */
  2351.                 (void) magnitude(const_express(&a));    /* skip yemin */
  2352.             }
  2353.             if (equals(c_token, ":")) {
  2354.                 c_token++;    /* skip ":" */
  2355.                 (void) magnitude(const_express(&a));    /* skip yemax */
  2356.             }
  2357.             if (equals(c_token, ":")) {
  2358.                 c_token++;    /* skip ":" */
  2359.                 (void) magnitude(const_express(&a));    /* skip wcol */
  2360.             }
  2361.             }
  2362.             if (isstring(c_token))
  2363.             c_token++;    /* skip format string */
  2364.         }
  2365.         } else {        /* function to plot */
  2366.         if (parametric)    /* working on x parametric function */
  2367.             xparam = 1 - xparam;
  2368.         dummy_func = &plot_func;
  2369.         plot_func.at = temp_at();    /* reparse function */
  2370.  
  2371.         is_log_func=parametric?(xparam?is_log_x:is_log_y):is_log_y;
  2372.         for (i = 0; i < samples; i++) {
  2373.             x = x_min + i * xdiff;
  2374.             /* if (is_log_x) PEM fix logscale x axis */
  2375.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2376.             (void) Gcomplex(&plot_func.dummy_values[0],
  2377.                    is_log_x ? pow(base_log_x, x) : x,
  2378.                    0.0);
  2379.  
  2380.             evaluate_at(plot_func.at, &a);
  2381.  
  2382.             if (undefined || (fabs(imag(&a)) > zero)) {
  2383.             this_plot->points[i].type = UNDEFINED;
  2384.             continue;
  2385.             }
  2386.             temp = real(&a);
  2387.             
  2388.             if (is_log_func && temp < 0.0) {
  2389.             this_plot->points[i].type = UNDEFINED;
  2390.             continue;
  2391.             }
  2392.             this_plot->points[i].x = x;
  2393.             this_plot->points[i].z = -1.0;  /* width of box not specified */
  2394.  
  2395.             if (is_log_func) {
  2396.             if (temp == 0.0) {
  2397.                 this_plot->points[i].type = OUTRANGE;
  2398.                 this_plot->points[i].y = -VERYLARGE;
  2399.                 continue;
  2400.             } else {
  2401.                 this_plot->points[i].y = log(temp)/log_base_log_y;
  2402.             }
  2403.             } else
  2404.             this_plot->points[i].y = temp;
  2405.  
  2406.             if (autoscale_ly || polar
  2407.             || inrange(temp, ymin, ymax)) {
  2408.             this_plot->points[i].type = INRANGE;
  2409.             /* When xparam is 1 we are not really computing y's! */
  2410.             if (!xparam && autoscale_ly) {
  2411.                 if (temp < ymin)
  2412.                 ymin = temp;
  2413.                 if (temp > ymax)
  2414.                 ymax = temp;
  2415.             }
  2416.             } else
  2417.             this_plot->points[i].type = OUTRANGE;
  2418.         }
  2419.         this_plot->p_count = i;    /* samples */
  2420.         }
  2421.  
  2422.         /* title was handled above */
  2423.         if (almost_equals(c_token, "t$itle")) {
  2424.         c_token++;
  2425.         c_token++;
  2426.         } else if (almost_equals(c_token, "not$itle")) {
  2427.             c_token++;
  2428.         }
  2429.         /* style was handled above */
  2430.         if (almost_equals(c_token, "w$ith")) {
  2431.         c_token++;
  2432.         c_token++;
  2433.         }
  2434.         /* line and point types were handled above */
  2435.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2436.         struct value    t;
  2437.         (void) real(const_express(&t));
  2438.         }
  2439.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2440.         struct value    t;
  2441.         (void) real(const_express(&t));
  2442.         }
  2443.         tp_ptr = &(this_plot->next_cp);    /* used below */
  2444.         this_plot = this_plot->next_cp;
  2445.     }
  2446.  
  2447.     if (equals(c_token, ","))
  2448.         c_token++;
  2449.     else
  2450.         break;
  2451.     }
  2452.  
  2453.     /* throw out all curve_points at end of list, that we don't need  */
  2454.     cp_free(*tp_ptr);
  2455.     *tp_ptr = NULL;
  2456.  
  2457.     /* if first_plot is NULL, we have no functions or data at all. This can
  2458.        happen, if you type "plot x=5", since x=5 is a variable assignment */
  2459.  
  2460.     if(first_plot==NULL) {
  2461.     int_error("no functions or data to plot", c_token);
  2462.     }
  2463.  
  2464.     if (fabs(ymax - ymin) < zero)
  2465.     /* if autoscale, widen range */
  2466.     if (autoscale_ly) {
  2467.         fprintf(stderr, "Warning: empty y range [%g:%g], ", ymin, ymax);
  2468.         if (fabs(ymin) < zero) {
  2469.         ymin = -1.;
  2470.         ymax = 1.;
  2471.         } else {
  2472.         /* expand range by 10% in either direction */
  2473.         ymin = ymin * 0.9;
  2474.         ymax = ymax * 1.1;
  2475.         }
  2476.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2477.     } else {
  2478.         int_error("y range is less than `zero`", c_token);
  2479.     }
  2480.  
  2481.     /* Now we finally know the real ymin and ymax */
  2482.     if (is_log_y) {
  2483.     y_min = log(ymin)/log_base_log_y;
  2484.     y_max = log(ymax)/log_base_log_y;
  2485.     } else {
  2486.     y_min = ymin;
  2487.     y_max = ymax;
  2488.     }
  2489.  
  2490.     /* Set a flag so capture is not invoked  by replot itself. -hmh */
  2491.     if (plot_token != -1) {
  2492.         capture(replot_line, plot_token, c_token);
  2493.         plot_token = -1;        
  2494.     }
  2495.   
  2496.     if (parametric) {
  2497.     /* Now put t and x ranges back before we actually plot anything. */
  2498.     ltmp = autoscale_lx;
  2499.     autoscale_lx = autoscale_lt;
  2500.     autoscale_lt = ltmp;
  2501.     temp = xmin;
  2502.     xmin = tmin;
  2503.     tmin = temp;
  2504.     temp = xmax;
  2505.     xmax = tmax;
  2506.     tmax = temp;
  2507.     if (some_data_files && autoscale_lx) {
  2508.         /*
  2509.          * Stop any further autoscaling in this case (may be a mistake,
  2510.          * have to consider what is really wanted some day in the
  2511.          * future--jdc).
  2512.          */
  2513.         autoscale_lx = 0;
  2514.     }
  2515.     /* Now actually fix the plot pairs to be single plots. */
  2516.     parametric_fixup(first_plot, &plot_num, &x_min, &x_max);
  2517.     }
  2518.     if (strcmp(term_tbl[term].name, "table") == 0)
  2519.     print_table();
  2520.     else
  2521.     do_plot(first_plot, plot_num, x_min, x_max, y_min, y_max);
  2522.     cp_free(first_plot);
  2523.     first_plot = NULL;
  2524. }
  2525.  
  2526. static void 
  2527. parse_title(crnt_param, start_token, end_token,
  2528.         xtitle, ytitle, this_plot, do_parse)
  2529. int crnt_param, start_token, end_token;
  2530. char **xtitle, **ytitle;
  2531. struct surface_points *this_plot;
  2532. TBOOLEAN do_parse;
  2533. {
  2534.     static char title[256];
  2535.  
  2536.     if (do_parse) {
  2537.     if (almost_equals(c_token, "t$itle")) {
  2538.         if (parametric) {
  2539.         if (crnt_param)
  2540.             int_error("\"title\" allowed only after parametric function fully specified",
  2541.                   c_token);
  2542.         else {
  2543.             /* Remove default title */
  2544.             if (*xtitle != NULL)
  2545.             (*xtitle)[0] = '\0';
  2546.             if (*ytitle != NULL)
  2547.             (*ytitle)[0] = '\0';
  2548.         }
  2549.         }
  2550.         c_token++;
  2551.         if (isstring(c_token)) {
  2552.         m_quote_capture(&(this_plot->title), c_token, c_token);
  2553.         } else {
  2554.         int_error("expecting \"title\" for plot", c_token);
  2555.         }
  2556.         c_token++;
  2557.     }  else if (almost_equals(c_token, "not$itle")) {
  2558.         c_token++;
  2559.     }  else {
  2560.         m_capture(&(this_plot->title), start_token, end_token);
  2561.         if (crnt_param == 1)
  2562.         *xtitle = this_plot->title;
  2563.         if (crnt_param == 2)
  2564.         *ytitle = this_plot->title;
  2565.     }
  2566.     }
  2567.     else {
  2568.     this_plot->title = alloc(strlen(title) + 1);
  2569.     strcpy(this_plot->title, title);
  2570.     }
  2571. }
  2572.  
  2573. /*
  2574.  * This parses the splot command after any range specifications. To support
  2575.  * autoscaling on the x/z axis, we want any data files to define the x/y
  2576.  * range, then to plot any functions using that range. We thus parse the
  2577.  * input twice, once to pick up the data files, and again to pick up the
  2578.  * functions. Definitions are processed twice, but that won't hurt.
  2579.  */
  2580. eval_3dplots()
  2581. {
  2582.     register int    i, j;
  2583.     register struct surface_points *this_plot=NULL, **tp_3d_ptr;
  2584.     register int    start_token, end_token;
  2585.     register int    begin_token;
  2586.     double          x_min, x_max, y_min, y_max, z_min, z_max;
  2587.     register double x, xdiff, xisodiff, y, ydiff, yisodiff, temp;
  2588.     static struct value a;
  2589.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2590.     int             plot_num, line_num, point_num, crnt_param = 0;    /* 0=z, 1=x, 2=y */
  2591.     char           *xtitle;
  2592.     char           *ytitle;
  2593.     void            parametric_3dfixup();
  2594.  
  2595.     /* Reset first_3dplot. This is usually done at the end of this function.
  2596.        If there is an error within this function, the memory is left allocated,
  2597.        since we cannot call sp_free if the list is incomplete */
  2598.     first_3dplot=NULL;
  2599.  
  2600.     if (autoscale_lz) {
  2601.     zmin = VERYLARGE;
  2602.     zmax = -VERYLARGE;
  2603.     } else if (is_log_z && (zmin <= 0.0 || zmax <= 0.0))
  2604.     int_error("z range must be above 0 for log scale!",
  2605.           NO_CARET);
  2606.  
  2607.     tp_3d_ptr = &(first_3dplot);
  2608.     plot_num = 0;
  2609.     line_num = 0;        /* default line type */
  2610.     point_num = 0;        /* default point type */
  2611.  
  2612.     xtitle = NULL;
  2613.     ytitle = NULL;
  2614.  
  2615.     begin_token = c_token;
  2616.  
  2617.     /*** First Pass: Read through data files ***/
  2618.     /*
  2619.      * This pass serves to set the x/yranges and to parse the command, as
  2620.      * well as filling in every thing except the function data. That is done
  2621.      * after the x/yrange is defined.
  2622.      */
  2623.     while (TRUE) {
  2624.     if (END_OF_COMMAND)
  2625.         int_error("function to plt3d expected", c_token);
  2626.  
  2627.     start_token = c_token;
  2628.  
  2629.     if (is_definition(c_token)) {
  2630.         define();
  2631.     } else {
  2632.         plot_num++;
  2633.  
  2634.         if (isstring(c_token)) {    /* data file to plot */
  2635.         int line_type = line_num,
  2636.             point_type = point_num,
  2637.             plot_style = data_style,
  2638.             first_mesh = TRUE;
  2639.  
  2640.         if (parametric && crnt_param != 0)
  2641.             int_error("previous parametric function not fully specified",
  2642.                   c_token);
  2643.  
  2644.         if (!some_data_files) {
  2645.             if (autoscale_lx) {
  2646.             xmin = VERYLARGE;
  2647.             xmax = -VERYLARGE;
  2648.             }
  2649.             if (autoscale_ly) {
  2650.             ymin = VERYLARGE;
  2651.             ymax = -VERYLARGE;
  2652.             }
  2653.         }
  2654.         some_data_files = TRUE;
  2655.  
  2656.         do {
  2657.             if (*tp_3d_ptr)
  2658.             this_plot = *tp_3d_ptr;
  2659.             else {        /* no memory malloc()'d there yet */
  2660.             /* Allocate enough isosamples and samples */
  2661.             this_plot = sp_alloc(0, 0, 0, 0);
  2662.             *tp_3d_ptr = this_plot;
  2663.             }
  2664.  
  2665.             this_plot->plot_type = DATA3D;
  2666.             end_token = c_token;
  2667.             /* this also parses index/using option */
  2668.             get_3ddata(this_plot);
  2669.  
  2670.             parse_title(crnt_param, start_token, end_token,
  2671.                 &xtitle, &ytitle, this_plot, first_mesh);
  2672.             if (!first_mesh) plot_num++;
  2673.  
  2674.             if (first_mesh) {
  2675.             if (almost_equals(c_token, "w$ith")) {
  2676.                 plot_style = this_plot->plot_style = get_style();
  2677.             }
  2678.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2679.                 struct value    t;
  2680.                 line_type = (int) real(const_express(&t)) - 1;
  2681.             }
  2682.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2683.                 struct value    t;
  2684.                 point_type = (int) real(const_express(&t)) - 1;
  2685.             }
  2686.             first_mesh = FALSE;
  2687.             }
  2688.  
  2689.             this_plot->line_type = line_type;
  2690.             this_plot->point_type = point_type;
  2691.             this_plot->plot_style = plot_style;
  2692.  
  2693.             tp_3d_ptr = &(this_plot->next_sp);
  2694.         }
  2695.         while (more_data_fp);
  2696.         } else {        /* function to plot */
  2697.         if (parametric)    /* Rotate between x/y/z axes */
  2698.             crnt_param = (crnt_param + 1) % 3;
  2699.         if (*tp_3d_ptr) {
  2700.             this_plot = *tp_3d_ptr;
  2701.             if (!hidden3d)
  2702.             sp_replace(this_plot, samples_1, iso_samples_1,
  2703.                                    samples_2, iso_samples_2);
  2704.             else
  2705.             sp_replace(this_plot, iso_samples_1, 0,
  2706.                                    0, iso_samples_2);
  2707.         } else {    /* no memory malloc()'d there yet */
  2708.             /* Allocate enough isosamples and samples */
  2709.             if (!hidden3d)
  2710.             this_plot = sp_alloc(samples_1, iso_samples_1,
  2711.                                              samples_2, iso_samples_2);
  2712.             else
  2713.             this_plot = sp_alloc(iso_samples_1, 0,
  2714.                                              0, iso_samples_2);
  2715.             *tp_3d_ptr = this_plot;
  2716.         }
  2717.  
  2718.         this_plot->plot_type = FUNC3D;
  2719.         this_plot->has_grid_topology = TRUE;
  2720.         this_plot->plot_style = func_style;
  2721.         dummy_func = &plot_func;
  2722.         plot_func.at = temp_at();
  2723.         /* ignore it for now */
  2724.         end_token = c_token - 1;
  2725.  
  2726.         parse_title(crnt_param, start_token, end_token,
  2727.                 &xtitle, &ytitle, this_plot, TRUE);
  2728.  
  2729.         this_plot->line_type = line_num;
  2730.         this_plot->point_type = point_num;
  2731.  
  2732.         if (almost_equals(c_token, "w$ith")) {
  2733.             this_plot->plot_style = get_style();
  2734.         }
  2735.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2736.             struct value    t;
  2737.             this_plot->line_type = (int) real(const_express(&t)) - 1;
  2738.         }
  2739.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2740.             struct value    t;
  2741.             this_plot->point_type = (int) real(const_express(&t)) - 1;
  2742.         }
  2743.  
  2744.         tp_3d_ptr = &(this_plot->next_sp);
  2745.         }
  2746.  
  2747.         if ((this_plot->plot_style == POINTSTYLE) ||
  2748.         (this_plot->plot_style == LINESPOINTS) ||
  2749.         (this_plot->plot_style == ERRORBARS))
  2750.         if (crnt_param == 0)
  2751.             point_num +=
  2752.             1 + (draw_contour != 0)
  2753.             + (hidden3d != 0);
  2754.         if (crnt_param == 0)
  2755.         line_num += 1 + (draw_contour != 0)
  2756.             + (hidden3d != 0);
  2757.     }
  2758.  
  2759.     if (equals(c_token, ","))
  2760.         c_token++;
  2761.     else
  2762.         break;
  2763.     }
  2764.  
  2765.     if (parametric && crnt_param != 0)
  2766.     int_error("parametric function not fully specified", NO_CARET);
  2767.  
  2768.     if (parametric) {
  2769.     /*
  2770.      * Swap u/v and x/y ranges for duration of these eval_plot
  2771.      * computations.
  2772.      */
  2773.     ltmp = autoscale_lx;
  2774.     autoscale_lx = autoscale_lu;
  2775.     autoscale_lu = ltmp;
  2776.     ltmp = autoscale_ly;
  2777.     autoscale_ly = autoscale_lv;
  2778.     autoscale_lv = ltmp;
  2779.     temp = xmin;
  2780.     xmin = umin;
  2781.     umin = temp;
  2782.     temp = xmax;
  2783.     xmax = umax;
  2784.     umax = temp;
  2785.     temp = ymin;
  2786.     ymin = vmin;
  2787.     vmin = temp;
  2788.     temp = ymax;
  2789.     ymax = vmax;
  2790.     vmax = temp;
  2791.     }
  2792.     /*** Second Pass: Evaluate the functions ***/
  2793.     /*
  2794.      * Everything is defined now, except the function data. We expect no
  2795.      * syntax errors, etc, since the above parsed it all. This makes the code
  2796.      * below simpler. If autoscale_ly, the yrange may still change.
  2797.      */
  2798.     if (xmin == xmax)
  2799.     if (autoscale_lx) {
  2800.         fprintf(stderr, "Warning: empty x range [%g:%g], ",
  2801.             xmin, xmax);
  2802.         if (xmin == 0.0) {
  2803.         /* completely arbitary */
  2804.         xmin = -1.;
  2805.         xmax = 1.;
  2806.         } else {
  2807.         /* expand range by 10% in either direction */
  2808.         xmin = xmin * 0.9;
  2809.         xmax = xmax * 1.1;
  2810.         }
  2811.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2812.     } else {
  2813.         int_error("x range is empty", c_token);
  2814.     }
  2815.  
  2816.     if (ymin == ymax)
  2817.     if (autoscale_ly) {
  2818.         fprintf(stderr, "Warning: empty y range [%g:%g], ",
  2819.             ymin, ymax);
  2820.         if (ymin == 0.0) {
  2821.         /* completely arbitary */
  2822.         ymin = -1.;
  2823.         ymax = 1.;
  2824.         } else {
  2825.         /* expand range by 10% in either direction */
  2826.         ymin = ymin * 0.9;
  2827.         ymax = ymax * 1.1;
  2828.         }
  2829.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2830.     } else {
  2831.         int_error("y range is empty", c_token);
  2832.     }
  2833.  
  2834.     /* give error if xrange badly set from missing datafile error */
  2835.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2836.     int_error("x range is invalid", c_token);
  2837.     }
  2838.     if (is_log_x) {
  2839.     if (xmin <= 0.0 || xmax <= 0.0)
  2840.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2841.     x_min = log(xmin)/log_base_log_x;
  2842.     x_max = log(xmax)/log_base_log_x;
  2843.     } else {
  2844.     x_min = xmin;
  2845.     x_max = xmax;
  2846.     }
  2847.  
  2848.     /* give error if yrange badly set from previous error */
  2849.     if (ymin == VERYLARGE || ymax == -VERYLARGE) {
  2850.     int_error("y range is invalid", c_token);
  2851.     }
  2852.     if (is_log_y) {
  2853.     if (ymin <= 0.0 || ymax <= 0.0)
  2854.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  2855.     y_min = log(ymin)/log_base_log_y;
  2856.     y_max = log(ymax)/log_base_log_y;
  2857.     } else {
  2858.     y_min = ymin;
  2859.     y_max = ymax;
  2860.     }
  2861.  
  2862.     if (samples_1 < 2 || samples_2 < 2 || iso_samples_1 < 2 || iso_samples_2 < 2)
  2863.     int_error("samples or iso_samples < 2. Must be at least 2.", NO_CARET);
  2864.  
  2865.     if (this_plot && this_plot->has_grid_topology && hidden3d) {
  2866.     xdiff = (x_max - x_min) / (iso_samples_1 - 1);
  2867.     ydiff = (y_max - y_min) / (iso_samples_2 - 1);
  2868.     } else {
  2869.     xdiff = (x_max - x_min) / (samples_1 - 1);
  2870.     ydiff = (y_max - y_min) / (samples_2 - 1);
  2871.     }
  2872.     xisodiff = (x_max - x_min) / (iso_samples_1 - 1);
  2873.     yisodiff = (y_max - y_min) / (iso_samples_2 - 1);
  2874.  
  2875.     this_plot = first_3dplot;
  2876.     c_token = begin_token;    /* start over */
  2877.  
  2878.     /* Read through functions */
  2879.     while (TRUE) {
  2880.     if (is_definition(c_token)) {
  2881.         define();
  2882.     } else {
  2883.         if (isstring(c_token)) {    /* data file to plot */
  2884.         /* ignore this now */
  2885.         c_token++;
  2886.         if (almost_equals(c_token, "i$ndex")) {
  2887.             struct value a;
  2888.             int index;
  2889.  
  2890.             c_token++;        /* skip "index" */
  2891.             index = (int) magnitude(const_express(&a));
  2892.         }
  2893.         if (almost_equals(c_token, "u$sing")) {
  2894.             c_token++;    /* skip "using" */
  2895.             if (!isstring(c_token)) {
  2896.             struct value    a;
  2897.             (void) magnitude(const_express(&a));    /* skip xcol */
  2898.             if (equals(c_token, ":")) {
  2899.                 c_token++;    /* skip ":" */
  2900.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2901.                 if (equals(c_token, ":")) {
  2902.                 c_token++;    /* skip ":" */
  2903.                 (void) magnitude(const_express(&a));    /* skip zcol */
  2904.                 }
  2905.             }
  2906.             }
  2907.             if (isstring(c_token))
  2908.             c_token++;    /* skip format string */
  2909.         }
  2910.         } else {        /* function to plot */
  2911.         struct iso_curve *this_iso = this_plot->iso_crvs;
  2912.         struct coordinate GPHUGE *points = this_iso->points;
  2913.         int num_sam_to_use, num_iso_to_use;
  2914.  
  2915.         if (parametric)
  2916.             crnt_param = (crnt_param + 1) % 3;
  2917.         dummy_func = &plot_func;
  2918.         plot_func.at = temp_at();    /* reparse function */
  2919.  
  2920.         num_iso_to_use = iso_samples_2;
  2921.         if (!(this_plot->has_grid_topology && hidden3d))
  2922.             num_sam_to_use = samples_1;
  2923.         else
  2924.             num_sam_to_use = iso_samples_1;
  2925.  
  2926.         is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  2927.         for (j = 0; j < num_iso_to_use; j++) {
  2928.             y = y_min + j * yisodiff;
  2929.             /* if (is_log_y) PEM fix logscale y axis */
  2930.             /* y = pow(log_base_log_y,y); 26-Sep-89 */
  2931.             (void) Gcomplex(&plot_func.dummy_values[1],
  2932.                    is_log_y ? pow(base_log_y, y) : y,
  2933.                    0.0);
  2934.  
  2935.             for (i = 0; i < num_sam_to_use; i++) {
  2936.             x = x_min + i * xdiff;
  2937.             /* if (is_log_x) PEM fix logscale x axis */
  2938.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2939.             (void) Gcomplex(&plot_func.dummy_values[0],
  2940.                        is_log_x ? pow(base_log_x, x) : x,
  2941.                        0.0);
  2942.  
  2943.             points[i].x = x;
  2944.             points[i].y = y;
  2945.  
  2946.             evaluate_at(plot_func.at, &a);
  2947.  
  2948.             if (undefined || (fabs(imag(&a)) > zero)) {
  2949.                 points[i].type = UNDEFINED;
  2950.                 continue;
  2951.             }
  2952.             temp = real(&a);
  2953.  
  2954.             if (is_log_func && temp < 0.0) {
  2955.                 points[i].type = UNDEFINED;
  2956.                 continue;
  2957.             }
  2958.             if (is_log_func) {
  2959.                 if (temp == 0.0) {
  2960.                 points[i].type = OUTRANGE;
  2961.                 points[i].z = -VERYLARGE;
  2962.                 continue;
  2963.                 } else {
  2964.                 points[i].z = log(temp)/log_base_log_z;
  2965.                 }
  2966.             } else
  2967.                 points[i].z = temp;
  2968.  
  2969.             if (autoscale_lz || inrange(temp, zmin, zmax)) {
  2970.                 points[i].type = INRANGE;
  2971.                 if (autoscale_lz) {
  2972.                 if (temp < zmin)
  2973.                     zmin = temp;
  2974.                 if (temp > zmax)
  2975.                     zmax = temp;
  2976.                 }
  2977.             } else
  2978.                 points[i].type = OUTRANGE;
  2979.             }
  2980.             this_iso->p_count = num_sam_to_use;
  2981.             this_iso = this_iso->next;
  2982.             points = this_iso? this_iso->points: NULL;
  2983.         }
  2984.  
  2985.         if (!(this_plot->has_grid_topology && hidden3d)) {
  2986.             num_iso_to_use = iso_samples_1;
  2987.             num_sam_to_use = samples_2;
  2988.             is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  2989.             for (i = 0; i < num_iso_to_use; i++) {
  2990.             x = x_min + i * xisodiff;
  2991.             /* if (is_log_x) PEM fix logscale x axis */
  2992.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2993.             (void) Gcomplex(&plot_func.dummy_values[0],
  2994.                        is_log_x ? pow(base_log_x, x) : x,
  2995.                        0.0);
  2996.  
  2997.             for (j = 0; j < num_sam_to_use; j++) {
  2998.                 y = y_min + j * ydiff;
  2999.                 /* if (is_log_y) PEM fix logscale y axis */
  3000.                 /* y = pow(base_log_y,y); 26-Sep-89 */
  3001.                 (void) Gcomplex(&plot_func.dummy_values[1],
  3002.                        is_log_y ? pow(base_log_y, y) : y,
  3003.                        0.0);
  3004.  
  3005.                 points[j].x = x;
  3006.                 points[j].y = y;
  3007.  
  3008.                 evaluate_at(plot_func.at, &a);
  3009.  
  3010.                 if (undefined || (fabs(imag(&a)) > zero)) {
  3011.                 points[j].type = UNDEFINED;
  3012.                 continue;
  3013.                 }
  3014.                 temp = real(&a);
  3015.  
  3016.                 if (is_log_func && temp < 0.0) {
  3017.                 points[j].type = UNDEFINED;
  3018.                 continue;
  3019.                 }
  3020.                 if (is_log_func) {
  3021.                 if (temp == 0.0) {
  3022.                     points[j].type = OUTRANGE;
  3023.                     points[j].z = -VERYLARGE;
  3024.                     continue;
  3025.                 } else {
  3026.                     points[j].z = log(temp)/log_base_log_z;
  3027.                 }
  3028.                 } else
  3029.                 points[j].z = temp;
  3030.  
  3031.                 if (autoscale_lz
  3032.                 || inrange(temp, zmin, zmax)) {
  3033.                 points[j].type = INRANGE;
  3034.                 if (autoscale_lz) {
  3035.                     if (temp < zmin)
  3036.                     zmin = temp;
  3037.                     if (temp > zmax)
  3038.                     zmax = temp;
  3039.                 }
  3040.                 } else
  3041.                 points[j].type = OUTRANGE;
  3042.             }
  3043.             this_iso->p_count = num_sam_to_use;
  3044.             this_iso = this_iso->next;
  3045.             points = this_iso ? this_iso->points : NULL;
  3046.             }
  3047.         }
  3048.         }
  3049.  
  3050.         /* title was handled above */
  3051.         if (almost_equals(c_token, "t$itle")) {
  3052.         c_token++;
  3053.         c_token++;
  3054.         } else if (almost_equals(c_token, "not$itle")) {
  3055.             c_token++;
  3056.         }
  3057.         /* style was handled above */
  3058.         if (almost_equals(c_token, "w$ith")) {
  3059.         c_token++;
  3060.         c_token++;
  3061.         }
  3062.         /* line and point types were handled above */
  3063.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  3064.         struct value    t;
  3065.         (void) real(const_express(&t));
  3066.         }
  3067.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  3068.         struct value    t;
  3069.         (void) real(const_express(&t));
  3070.         }
  3071.         this_plot = this_plot->next_sp;
  3072.     }
  3073.  
  3074.     if (equals(c_token, ","))
  3075.         c_token++;
  3076.     else
  3077.         break;
  3078.     }
  3079.  
  3080.     /* if first_3dplot is NULL, we have no functions or data at all. This can
  3081.        happen, if you type "splot x=5", since x=5 is a variable assignment */
  3082.  
  3083.     if(first_3dplot==NULL) {
  3084.     int_error("no functions or data to plot", c_token);
  3085.     }
  3086.  
  3087.     if (fabs(zmax - zmin) < zero)
  3088.     /* if autoscale, widen range */
  3089.     if (autoscale_lz) {
  3090.         fprintf(stderr, "Warning: empty z range [%g:%g], ", zmin, zmax);
  3091.         if (fabs(zmin) < zero) {
  3092.         zmin = -1.;
  3093.         zmax = 1.;
  3094.         } else {
  3095.         /* expand range by 10% in either direction */
  3096.         zmin = zmin * 0.9;
  3097.         zmax = zmax * 1.1;
  3098.         }
  3099.         fprintf(stderr, "adjusting to [%g:%g]\n", zmin, zmax);
  3100.     } else {
  3101.         int_error("z range is less than `zero`", c_token);
  3102.     }
  3103.  
  3104.     /* Now we finally know the real zmin and zmax */
  3105.     if (is_log_z) {
  3106.     if (zmin <= 0.0 || zmax <= 0.0)
  3107.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3108.     z_min = log(zmin)/log_base_log_z;
  3109.     z_max = log(zmax)/log_base_log_z;
  3110.     } else {
  3111.     z_min = zmin;
  3112.     z_max = zmax;
  3113.     }
  3114.  
  3115.     /* Set a flag so capture is not invoked by replot itself. -hmh */
  3116.     if (plot_token != -1) {
  3117.     capture(replot_line, plot_token, c_token);
  3118.     plot_token = -1;
  3119.     }
  3120.  
  3121.     if (parametric) {
  3122.     /* Now put u/v and x/y ranges back before we actually plot anything. */
  3123.     ltmp = autoscale_lx;
  3124.     autoscale_lx = autoscale_lu;
  3125.     autoscale_lu = ltmp;
  3126.     ltmp = autoscale_ly;
  3127.     autoscale_ly = autoscale_lv;
  3128.     autoscale_lv = ltmp;
  3129.     temp = xmin;
  3130.     xmin = umin;
  3131.     umin = temp;
  3132.     temp = xmax;
  3133.     xmax = umax;
  3134.     umax = temp;
  3135.     temp = ymin;
  3136.     ymin = vmin;
  3137.     vmin = temp;
  3138.     temp = ymax;
  3139.     ymax = vmax;
  3140.     vmax = temp;
  3141.  
  3142.     /* Now actually fix the plot triplets to be single plots. */
  3143.     parametric_3dfixup(first_3dplot, &plot_num,
  3144.                &x_min, &x_max, &y_min, &y_max,
  3145.                &z_min, &z_max);
  3146.     if (is_log_x) {
  3147.         if (x_min <= 0.0 || x_max <= 0.0)
  3148.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  3149.         x_min = log(x_min)/log_base_log_x;
  3150.         x_max = log(x_max)/log_base_log_x;
  3151.     }
  3152.     if (is_log_y) {
  3153.         if (y_min <= 0.0 || y_max <= 0.0)
  3154.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  3155.         y_min = log(y_min)/log_base_log_y;
  3156.         y_max = log(y_max)/log_base_log_y;
  3157.     }
  3158.     if (is_log_z) {
  3159.         if (z_min <= 0.0 || z_max <= 0.0)
  3160.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3161.         z_min = log(z_min)/log_base_log_z;
  3162.         z_max = log(z_max)/log_base_log_z;
  3163.     }
  3164.     }
  3165.  
  3166.     /* Filter out empty meshes. */
  3167.     while (first_3dplot &&
  3168.        first_3dplot->num_iso_read == 0 &&
  3169.        first_3dplot->plot_type == DATA3D) {
  3170.     struct surface_points *plt = first_3dplot->next_sp;
  3171.  
  3172.     first_3dplot->next_sp = NULL;
  3173.     sp_free(first_3dplot);
  3174.     plot_num--;
  3175.     first_3dplot = plt;
  3176.     }
  3177.     if (first_3dplot != NULL) {
  3178.     struct surface_points *plt1, *plt2;
  3179.     
  3180.     for (plt1 = first_3dplot, plt2 = plt1->next_sp; plt2 != NULL; ) {
  3181.         if (plt2->num_iso_read == 0 && plt2->plot_type == DATA3D) {
  3182.         plt2 = plt2->next_sp;
  3183.         plt1->next_sp->next_sp = NULL;
  3184.         sp_free(plt1->next_sp);
  3185.         plot_num--;
  3186.         plt1->next_sp = plt2;
  3187.         }
  3188.         else {
  3189.         plt1 = plt2;
  3190.         plt2 = plt2->next_sp;
  3191.         }
  3192.     }
  3193.     }
  3194.     if (first_3dplot == NULL)
  3195.     int_error("no data found in file", NO_CARET);
  3196.  
  3197.     /* Creates contours if contours are to be plotted as well. */
  3198.     if (draw_contour) {
  3199.     for (this_plot = first_3dplot, i = 0;
  3200.          i < plot_num;
  3201.          this_plot = this_plot->next_sp, i++) {
  3202.         if (this_plot->contours) {
  3203.         struct gnuplot_contours *cntr, *cntrs = this_plot->contours;
  3204.  
  3205.         while (cntrs) {
  3206.             cntr = cntrs;
  3207.             cntrs = cntrs->next;
  3208.             gpfarfree(cntr->coords);
  3209.             free(cntr);
  3210.         }
  3211.         }
  3212.         /* Make sure this one can be contoured. */
  3213.         if (!this_plot->has_grid_topology) {
  3214.         this_plot->contours = NULL;
  3215.         fprintf(stderr,"Notice: cannot contour non grid data!\n");
  3216.         /* changed from int_error by recommendation of rkc@xn.ll.mit.edu */
  3217.         }
  3218.         else if (this_plot->plot_type == DATA3D)
  3219.         this_plot->contours = contour(
  3220.                          this_plot->num_iso_read,
  3221.                          this_plot->iso_crvs,
  3222.                          contour_levels, contour_pts,
  3223.                          contour_kind, contour_order,
  3224.                          levels_kind, levels_list);
  3225.         else
  3226.         this_plot->contours = contour(iso_samples_2,
  3227.                           this_plot->iso_crvs,
  3228.                           contour_levels, contour_pts,
  3229.                           contour_kind, contour_order,
  3230.                           levels_kind, levels_list);
  3231.     }
  3232.     }
  3233.     if (strcmp(term_tbl[term].name, "table") == 0)
  3234.     print_3dtable(plot_num);
  3235.     else
  3236.     do_3dplot(first_3dplot, plot_num, x_min, x_max, y_min, y_max, z_min, z_max);
  3237.     sp_free(first_3dplot);
  3238.     first_3dplot = NULL;
  3239. }
  3240.  
  3241. done(status)
  3242.     int             status;
  3243. {
  3244.     if (term && term_init)
  3245.     (*term_tbl[term].reset) ();
  3246. #ifdef vms
  3247.     vms_reset();
  3248. #endif
  3249.     exit(status);
  3250. }
  3251.  
  3252. void 
  3253. parametric_fixup(start_plot, plot_num, x_min, x_max)
  3254.     struct curve_points *start_plot;
  3255.     int            *plot_num;
  3256.     double         *x_min, *x_max;
  3257. /*
  3258.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3259.  * list (which are gauranteed to occur in (x,y) pairs while preserving the
  3260.  * non-FUNC type plots intact.  This means we have to work our way through
  3261.  * various lists.  Examples (hand checked): start_plot:F1->F2->NULL ==>
  3262.  * F2->NULL start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F2->F4->F6->NULL
  3263.  * start_plot:F1->F2->D1->D2->F3->F4->D3->NULL ==> F2->D1->D2->F4->D3->NULL
  3264.  * 
  3265.  * Of course, the more interesting work is to move the y values of the x
  3266.  * function to become the x values of the y function (checking the mins and
  3267.  * maxs as we go along).
  3268.  */
  3269. {
  3270.     struct curve_points *xp, *new_list, *yp = start_plot, *tmp, *free_list,
  3271.                    *free_head = NULL;
  3272.     int             i, tlen, curve;
  3273.     char           *new_title;
  3274.     double          lxmin, lxmax, temp;
  3275.  
  3276.     if (autoscale_lx) {
  3277.     lxmin = VERYLARGE;
  3278.     lxmax = -VERYLARGE;
  3279.     } else {
  3280.     lxmin = xmin;
  3281.     lxmax = xmax;
  3282.     }
  3283.  
  3284.     /*
  3285.      * Ok, go through all the plots and move FUNC types together.  Note: this
  3286.      * originally was written to look for a NULL next pointer, but gnuplot
  3287.      * wants to be sticky in grabbing memory and the right number of items in
  3288.      * the plot list is controlled by the plot_num variable.
  3289.      * 
  3290.      * Since gnuplot wants to do this sticky business, a free_list of
  3291.      * curve_points is kept and then tagged onto the end of the plot list as
  3292.      * this seems more in the spirit of the original memory behavior than
  3293.      * simply freeing the memory.  I'm personally not convinced this sort of
  3294.      * concern is worth it since the time spent computing points seems to
  3295.      * dominate any garbage collecting that might be saved here...
  3296.      */
  3297.     new_list = xp = start_plot;
  3298.     yp = xp->next_cp;
  3299.     curve = 0;
  3300.  
  3301.     for (; curve < *plot_num; xp = xp->next_cp, yp = yp ? yp->next_cp : yp, curve++) {
  3302.     if (xp->plot_type != FUNC) {
  3303.         continue;
  3304.     }
  3305.     /* Here's a FUNC parametric function defined as two parts. */
  3306.     --(*plot_num);
  3307.     /*
  3308.      * Go through all the points assigning the y's from xp to be the x's
  3309.      * for yp.  Check max's and min's as you go.
  3310.      */
  3311.     for (i = 0; i < yp->p_count; ++i) {
  3312.         /*
  3313.          * Throw away excess xp points, mark excess yp points as
  3314.          * OUTRANGE.
  3315.          */
  3316.         if (i > xp->p_count) {
  3317.         yp->points[i].type = OUTRANGE;
  3318.         continue;
  3319.         }
  3320.         /*
  3321.          * Just as we had to do when we computed y values--now check that
  3322.          * x's (computed parametrically) are in the permitted ranges as
  3323.          * well.
  3324.          */
  3325.         temp = xp->points[i].y;    /* New x value for yp function. */
  3326.         yp->points[i].x = temp;
  3327.         /* Handle undefined values differently from normal ranges. */
  3328.         if (xp->points[i].type == UNDEFINED)
  3329.         yp->points[i].type = xp->points[i].type;
  3330.         if (autoscale_lx || polar
  3331.         || inrange(temp, lxmin, lxmax)) {
  3332.         if (autoscale_lx && temp < lxmin)
  3333.             lxmin = temp;
  3334.         if (autoscale_lx && temp > lxmax)
  3335.             lxmax = temp;
  3336.         } else
  3337.         yp->points[i].type = OUTRANGE;    /* Due to x value. */
  3338.     }
  3339.     /* Ok, fix up the title to include both the xp and yp plots. */
  3340.     if (xp->title && xp->title[0] != '\0') {
  3341.         tlen = strlen(yp->title) + strlen(xp->title) + 3;
  3342.         new_title = alloc((unsigned long) tlen, "string");
  3343.         strcpy(new_title, xp->title);
  3344.         strcat(new_title, ", ");    /* + 2 */
  3345.         strcat(new_title, yp->title);    /* + 1 = + 3 */
  3346.         free(yp->title);
  3347.         yp->title = new_title;
  3348.     }
  3349.     /* Eliminate the first curve (xparam) and just use the second. */
  3350.     if (xp == start_plot) {
  3351.         /* Simply nip off the first element of the list. */
  3352.         new_list = first_plot = yp;
  3353.         xp = xp->next_cp;
  3354.         if (yp->next_cp != NULL)
  3355.         yp = yp->next_cp;
  3356.         /* Add start_plot to the free_list. */
  3357.         if (free_head == NULL) {
  3358.         free_list = free_head = start_plot;
  3359.         free_head->next_cp = NULL;
  3360.         } else {
  3361.         free_list->next_cp = start_plot;
  3362.         start_plot->next_cp = NULL;
  3363.         free_list = start_plot;
  3364.         }
  3365.     } else {
  3366.         /* Here, remove the xp node and replace it with the yp node. */
  3367.         tmp = xp;
  3368.         /* Pass over any data files that might have been in place. */
  3369.         while (new_list->next_cp && new_list->next_cp != xp)
  3370.         new_list = new_list->next_cp;
  3371.         new_list->next_cp = yp;
  3372.         new_list = new_list->next_cp;
  3373.         xp = xp->next_cp;
  3374.         if (yp->next_cp != NULL)
  3375.         yp = yp->next_cp;
  3376.         /* Add tmp to the free_list. */
  3377.         tmp->next_cp = NULL;
  3378.         if (free_head == NULL) {
  3379.         free_list = free_head = tmp;
  3380.         } else {
  3381.         free_list->next_cp = tmp;
  3382.         free_list = tmp;
  3383.         }
  3384.     }
  3385.     }
  3386.     /* Ok, stick the free list at the end of the curve_points plot list. */
  3387.     while (new_list->next_cp != NULL)
  3388.     new_list = new_list->next_cp;
  3389.     new_list->next_cp = free_head;
  3390.  
  3391.     /* Report the overall graph mins and maxs. */
  3392.     *x_min = lxmin;
  3393.     *x_max = lxmax;
  3394. }
  3395.  
  3396. void 
  3397. parametric_3dfixup(start_plot, plot_num, x_min, x_max, y_min, y_max,
  3398.            z_min, z_max)
  3399.     struct surface_points *start_plot;
  3400.     int            *plot_num;
  3401.     double         *x_min, *x_max, *y_min, *y_max, *z_min, *z_max;
  3402. /*
  3403.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3404.  * list (which are gauranteed to occur in (x,y,z) triplets while preserving
  3405.  * the non-FUNC type plots intact.  This means we have to work our way
  3406.  * through various lists.  Examples (hand checked):
  3407.  * start_plot:F1->F2->F3->NULL ==> F3->NULL
  3408.  * start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F3->F6->NULL
  3409.  * start_plot:F1->F2->F3->D1->D2->F4->F5->F6->D3->NULL ==>
  3410.  * F3->D1->D2->F6->D3->NULL
  3411.  */
  3412. {
  3413.     struct surface_points *xp, *yp, *zp, *new_list, *tmp, *free_list, *free_head = NULL;
  3414.     struct iso_curve *icrvs, *xicrvs, *yicrvs, *zicrvs;
  3415.     int             i, tlen, surface;
  3416.     char           *new_title;
  3417.     double          lxmin, lxmax, lymin, lymax, lzmin, lzmax;
  3418.  
  3419.     if (autoscale_lx) {
  3420.     lxmin = VERYLARGE;
  3421.     lxmax = -VERYLARGE;
  3422.     } else {
  3423.     lxmin = xmin;
  3424.     lxmax = xmax;
  3425.     }
  3426.  
  3427.     if (autoscale_ly) {
  3428.     lymin = VERYLARGE;
  3429.     lymax = -VERYLARGE;
  3430.     } else {
  3431.     lymin = ymin;
  3432.     lymax = ymax;
  3433.     }
  3434.  
  3435.     if (autoscale_lz) {
  3436.     lzmin = VERYLARGE;
  3437.     lzmax = -VERYLARGE;
  3438.     } else {
  3439.     lzmin = zmin;
  3440.     lzmax = zmax;
  3441.     }
  3442.  
  3443.     /*
  3444.      * Ok, go through all the plots and move FUNC3D types together.  Note:
  3445.      * this originally was written to look for a NULL next pointer, but
  3446.      * gnuplot wants to be sticky in grabbing memory and the right number of
  3447.      * items in the plot list is controlled by the plot_num variable.
  3448.      * 
  3449.      * Since gnuplot wants to do this sticky business, a free_list of
  3450.      * surface_points is kept and then tagged onto the end of the plot list
  3451.      * as this seems more in the spirit of the original memory behavior than
  3452.      * simply freeing the memory.  I'm personally not convinced this sort of
  3453.      * concern is worth it since the time spent computing points seems to
  3454.      * dominate any garbage collecting that might be saved here...
  3455.      */
  3456.     new_list = xp = start_plot;
  3457.     for (surface = 0; surface < *plot_num; surface++) {
  3458.     if (xp->plot_type != FUNC3D) {
  3459.         icrvs = xp->iso_crvs;
  3460.  
  3461.         while (icrvs) {
  3462.         struct coordinate GPHUGE *points = icrvs->points;
  3463.  
  3464.         for (i = 0; i < icrvs->p_count; ++i) {
  3465.             if (lxmin > points[i].x)
  3466.             lxmin = points[i].x;
  3467.             if (lxmax < points[i].x)
  3468.             lxmax = points[i].x;
  3469.             if (lymin > points[i].y)
  3470.             lymin = points[i].y;
  3471.             if (lymax < points[i].y)
  3472.             lymax = points[i].y;
  3473.             if (lzmin > points[i].z)
  3474.             lzmin = points[i].z;
  3475.             if (lzmax < points[i].z)
  3476.             lzmax = points[i].z;
  3477.         }
  3478.  
  3479.         icrvs = icrvs->next;
  3480.         }
  3481.         xp = xp->next_sp;
  3482.         continue;
  3483.     }
  3484.     yp = xp->next_sp;
  3485.     zp = yp->next_sp;
  3486.  
  3487.     /* Here's a FUNC3D parametric function defined as three parts. */
  3488.     (*plot_num) -= 2;
  3489.     /*
  3490.      * Go through all the points and assign the x's and y's from xp and
  3491.      * yp to zp.  Check max's and min's as you go.
  3492.      */
  3493.     xicrvs = xp->iso_crvs;
  3494.     yicrvs = yp->iso_crvs;
  3495.     zicrvs = zp->iso_crvs;
  3496.     while (zicrvs) {
  3497.         struct coordinate GPHUGE *xpoints = xicrvs->points, GPHUGE *ypoints = yicrvs->points, GPHUGE *zpoints = zicrvs->points;
  3498.         for (i = 0; i < zicrvs->p_count; ++i) {
  3499.         zpoints[i].x = xpoints[i].z;
  3500.         zpoints[i].y = ypoints[i].z;
  3501.  
  3502.         if (lxmin > zpoints[i].x)
  3503.             lxmin = zpoints[i].x;
  3504.         if (lxmax < zpoints[i].x)
  3505.             lxmax = zpoints[i].x;
  3506.         if (lymin > zpoints[i].y)
  3507.             lymin = zpoints[i].y;
  3508.         if (lymax < zpoints[i].y)
  3509.             lymax = zpoints[i].y;
  3510.         if (lzmin > zpoints[i].z)
  3511.             lzmin = zpoints[i].z;
  3512.         if (lzmax < zpoints[i].z)
  3513.             lzmax = zpoints[i].z;
  3514.         }
  3515.         xicrvs = xicrvs->next;
  3516.         yicrvs = yicrvs->next;
  3517.         zicrvs = zicrvs->next;
  3518.     }
  3519.  
  3520.     /* Ok, fix up the title to include xp and yp plots. */
  3521.     if ((xp->title && xp->title[0] != '\0') ||
  3522.         (yp->title && yp->title[0] != '\0')) {
  3523.         tlen = (xp->title ? strlen(xp->title) : 0) +
  3524.         (yp->title ? strlen(yp->title) : 0) +
  3525.         (zp->title ? strlen(zp->title) : 0) + 5;
  3526.         new_title = alloc((unsigned long) tlen, "string");
  3527.         new_title[0] = 0;
  3528.         if (xp->title) {
  3529.         strcat(new_title, xp->title);
  3530.         strcat(new_title, ", ");    /* + 2 */
  3531.         }
  3532.         if (yp->title) {
  3533.         strcat(new_title, yp->title);
  3534.         strcat(new_title, ", ");    /* + 2 */
  3535.         }
  3536.         if (zp->title) {
  3537.         strcat(new_title, zp->title);
  3538.         }
  3539.         free(zp->title);
  3540.         zp->title = new_title;
  3541.     }
  3542.     /*
  3543.      * Eliminate the first two surfaces (xp and yp) and just use the
  3544.      * third.
  3545.      */
  3546.     if (xp == start_plot) {
  3547.         /* Simply nip off the first two elements of the list. */
  3548.         new_list = first_3dplot = zp;
  3549.         xp = zp->next_sp;
  3550.         /* Add xp and yp to the free_list. */
  3551.         if (free_head == NULL) {
  3552.         free_head = start_plot;
  3553.         } else {
  3554.         free_list->next_sp = start_plot;
  3555.         }
  3556.         free_list = start_plot->next_sp;
  3557.         free_list->next_sp = NULL;
  3558.     } else {
  3559.         /*
  3560.          * Here, remove the xp,yp nodes and replace them with the zp
  3561.          * node.
  3562.          */
  3563.         tmp = xp;
  3564.         /* Pass over any data files that might have been in place. */
  3565.         while (new_list->next_sp && new_list->next_sp != xp)
  3566.         new_list = new_list->next_sp;
  3567.         new_list->next_sp = zp;
  3568.         new_list = zp;
  3569.         xp = zp->next_sp;
  3570.         /* Add tmp to the free_list. */
  3571.         if (free_head == NULL) {
  3572.         free_head = tmp;
  3573.         } else {
  3574.         free_list->next_sp = tmp;
  3575.         }
  3576.         free_list = tmp->next_sp;
  3577.         free_list->next_sp = NULL;
  3578.     }
  3579.     }
  3580.     /* Ok, stick the free list at the end of the surface_points plot list. */
  3581.     while (new_list->next_sp != NULL)
  3582.     new_list = new_list->next_sp;
  3583.     new_list->next_sp = free_head;
  3584.     if (lxmax - lxmin < zero) {
  3585.     if (fabs(lxmax) < zero) {
  3586.         lxmin = -1.0;
  3587.         lxmax = 1.0;
  3588.     } else {
  3589.         lxmin *= 0.9;
  3590.         lxmax *= 1.1;
  3591.     }
  3592.     }
  3593.     if (lymax - lymin < zero) {
  3594.     if (fabs(lymax) < zero) {
  3595.         lymin = -1.0;
  3596.         lymax = 1.0;
  3597.     } else {
  3598.         lymin *= 0.9;
  3599.         lymax *= 1.1;
  3600.     }
  3601.     }
  3602.     if (lzmax - lzmin < zero) {
  3603.     if (fabs(lzmax) < zero) {
  3604.         lzmin = -1.0;
  3605.         lzmax = 1.0;
  3606.     } else {
  3607.         lzmin *= 0.9;
  3608.         lzmax *= 1.1;
  3609.     }
  3610.     }
  3611.     /* Report the overall graph mins and maxs. */
  3612.     if (autoscale_lx) {
  3613.     *x_min = (is_log_x ? pow(base_log_x, lxmin) : lxmin);
  3614.     *x_max = (is_log_x ? pow(base_log_x, lxmax) : lxmax);
  3615.     } else {
  3616.     *x_min = xmin;
  3617.     *x_max = xmax;
  3618.     }
  3619.     if (autoscale_ly) {
  3620.     *y_min = (is_log_y ? pow(base_log_y, lymin) : lymin);
  3621.     *y_max = (is_log_y ? pow(base_log_y, lymax) : lymax);
  3622.     } else {
  3623.     *y_min = ymin;
  3624.     *y_max = ymax;
  3625.     }
  3626.     if (autoscale_lz) {
  3627.     *z_min = (is_log_z ? pow(base_log_z, lzmin) : lzmin);
  3628.     *z_max = (is_log_z ? pow(base_log_z, lzmax) : lzmax);
  3629.     } else {
  3630.     *z_min = zmin;
  3631.     *z_max = zmax;
  3632.     }
  3633. }
  3634.  
  3635. #ifdef AMIGA_SC_6_1
  3636. void 
  3637. sleep(delay)
  3638.     unsigned int    delay;
  3639. {
  3640.     Delay(50 * delay);
  3641. }
  3642. #endif
  3643.  
  3644. #ifdef AMIGA_AC_5
  3645. void 
  3646. sleep(delay)
  3647.     unsigned int    delay;
  3648. {
  3649.     unsigned long   time_is_up;
  3650.     time_is_up = time(NULL) + (unsigned long) delay;
  3651.     while (time(NULL) < time_is_up)
  3652.      /* wait */ ;
  3653. }
  3654. #endif
  3655.  
  3656. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3657. #if (!defined(__TURBOC__) && !defined(__EMX__) && !defined(DJGPP)) || defined(_Windows) /* Turbo C already has sleep() */
  3658. #ifndef __ZTC__            /* ZTC already has usleep() */
  3659. /* kludge to provide sleep() for msc 5.1 */
  3660. void 
  3661. sleep(delay)
  3662.     unsigned int    delay;
  3663. {
  3664.     unsigned long   time_is_up;
  3665.     time_is_up = time(NULL) + (unsigned long) delay;
  3666.     while (time(NULL) < time_is_up)
  3667.      /* wait */ ;
  3668. }
  3669. #endif                /* not ZTC */
  3670. #endif                /* (!TURBOC && !__EMX__ && !DJGPP) or _Windows */
  3671. #endif                /* MSDOS || _Windows*/
  3672.  
  3673.  
  3674. /* Support for input, shell, and help for various systems */
  3675.  
  3676. #ifdef vms
  3677.  
  3678. #include <descrip.h>
  3679. #include <rmsdef.h>
  3680. #include <errno.h>
  3681. #include <smgdef.h>
  3682. #include <smgmsg.h>
  3683.  
  3684. extern          lib$get_input(), lib$put_output();
  3685. extern          smg$read_composed_line();
  3686.  
  3687. int             vms_len;
  3688.  
  3689. unsigned int    status[2] =
  3690. {1, 0};
  3691.  
  3692. static char     help[MAX_LINE_LEN + 1] = "gnuplot";
  3693.  
  3694. $DESCRIPTOR(prompt_desc, PROMPT);
  3695. $DESCRIPTOR(line_desc, input_line);
  3696.  
  3697. $DESCRIPTOR(help_desc, help);
  3698. $DESCRIPTOR(helpfile_desc, "GNUPLOT$HELP");
  3699.  
  3700.  
  3701. read_line(prompt)
  3702.     char           *prompt;
  3703. {
  3704.     int             more, start = 0;
  3705.     char            expand_prompt[40];
  3706.  
  3707.     prompt_desc.dsc$w_length = strlen(prompt);
  3708.     prompt_desc.dsc$a_pointer = prompt;
  3709.     (void) strcpy(expand_prompt, "_");
  3710.     (void) strncat(expand_prompt, prompt, 38);
  3711.     do {
  3712.     line_desc.dsc$w_length = MAX_LINE_LEN - start;
  3713.     line_desc.dsc$a_pointer = &input_line[start];
  3714.     switch (status[1] = smg$read_composed_line(&vms_vkid, 0, &line_desc, &prompt_desc, &vms_len)) {
  3715.     case SMG$_EOF:
  3716.         done(IO_SUCCESS);    /* ^Z isn't really an error */
  3717.         break;
  3718.     case RMS$_TNS:        /* didn't press return in time */
  3719.         vms_len--;        /* skip the last character */
  3720.         break;        /* and parse anyway */
  3721.     case RMS$_BES:        /* Bad Escape Sequence */
  3722.     case RMS$_PES:        /* Partial Escape Sequence */
  3723.         sys$putmsg(status);
  3724.         vms_len = 0;    /* ignore the line */
  3725.         break;
  3726.     case SS$_NORMAL:
  3727.         break;        /* everything's fine */
  3728.     default:
  3729.         done(status[1]);    /* give the error message */
  3730.     }
  3731.     start += vms_len;
  3732.     input_line[start] = '\0';
  3733.     inline_num++;
  3734.     if (input_line[start - 1] == '\\') {
  3735.         /* Allow for a continuation line. */
  3736.         prompt_desc.dsc$w_length = strlen(expand_prompt);
  3737.         prompt_desc.dsc$a_pointer = expand_prompt;
  3738.         more = 1;
  3739.         --start;
  3740.     } else {
  3741.         line_desc.dsc$w_length = strlen(input_line);
  3742.         line_desc.dsc$a_pointer = input_line;
  3743.         more = 0;
  3744.     }
  3745.     } while (more);
  3746.     return 0;
  3747. }
  3748.  
  3749.  
  3750. do_help()
  3751. {
  3752.     help_desc.dsc$w_length = strlen(help);
  3753.     if ((vaxc$errno = lbr$output_help(lib$put_output, 0, &help_desc,
  3754.                &helpfile_desc, 0, lib$get_input)) != SS$_NORMAL)
  3755.     os_error("can't open GNUPLOT$HELP", NO_CARET);
  3756. }
  3757.  
  3758.  
  3759. do_shell()
  3760. {
  3761.     if ((vaxc$errno = lib$spawn()) != SS$_NORMAL) {
  3762.     os_error("spawn error", NO_CARET);
  3763.     }
  3764. }
  3765.  
  3766.  
  3767. do_system()
  3768. {
  3769.     input_line[0] = ' ';    /* an embarrassment, but... */
  3770.  
  3771.     if ((vaxc$errno = lib$spawn(&line_desc)) != SS$_NORMAL)
  3772.     os_error("spawn error", NO_CARET);
  3773.  
  3774.     (void) putc('\n', stderr);
  3775. }
  3776.  
  3777. #else                /* vms */
  3778.  
  3779. #ifdef _Windows
  3780. do_help()
  3781. {
  3782.     if (END_OF_COMMAND)
  3783.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_INDEX,(DWORD)NULL);
  3784.     else {
  3785.         char buf[128];
  3786.         int start = c_token++;
  3787.         while (!(END_OF_COMMAND))
  3788.             c_token++;
  3789.         capture(buf, start, c_token-1);
  3790.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_PARTIALKEY,(DWORD)buf);
  3791.     }
  3792. }
  3793. #else
  3794.  
  3795. /*
  3796.  * do_help: (not VMS, although it would work) Give help to the user. It
  3797.  * parses the command line into helpbuf and supplies help for that string.
  3798.  * Then, if there are subtopics available for that key, it prompts the user
  3799.  * with this string. If more input is given, do_help is called recursively,
  3800.  * with the argument the index of null character in the string. Thus a more
  3801.  * specific help can be supplied. This can be done repeatedly. If null input
  3802.  * is given, the function returns, effecting a backward climb up the tree.
  3803.  * David Kotz (David.Kotz@Dartmouth.edu) 10/89
  3804.  */
  3805.  
  3806. do_help()
  3807. {
  3808.     static char    *helpbuf = NULL;
  3809.     static char    *prompt = NULL;
  3810.     int             base;    /* index of first char AFTER help string */
  3811.     int             len;    /* length of current help string */
  3812.     TBOOLEAN         more_help;
  3813.     TBOOLEAN         only;    /* TRUE if only printing subtopics */
  3814.     int             subtopics;    /* 0 if no subtopics for this topic */
  3815.     int             start;    /* starting token of help string */
  3816.     char           *help_ptr;    /* name of help file */
  3817. #ifdef ATARI
  3818.     static char    help_fname[256]=""; /* keep helpfilename across calls */
  3819. #endif
  3820.  
  3821.     if ((help_ptr = getenv("GNUHELP")) == (char *) NULL)
  3822. #ifndef ATARI
  3823.     /* if can't find environment variable then just use HELPFILE */
  3824.     help_ptr = HELPFILE;
  3825. #else
  3826.     /* try whether we can find the helpfile via shell_find. If not, just
  3827.        use the default. (tnx Andreas) */
  3828.  
  3829. #ifdef sequent
  3830.     if( !index( HELPFILE, ':' ) && !index( HELPFILE, '/' ) &&
  3831.         !index( HELPFILE, '\\' ) ) {
  3832. #else
  3833.     if( !strchr( HELPFILE, ':' ) && !strchr( HELPFILE, '/' ) &&
  3834.         !strchr( HELPFILE, '\\' ) ) {
  3835. #endif
  3836.         if( strlen(help_fname)==0 ) {
  3837.         strcpy( help_fname, HELPFILE );
  3838.         if( shel_find( help_fname )==0 ) {
  3839.             strcpy( help_fname, HELPFILE );
  3840.         }
  3841.         }
  3842.         help_ptr=help_fname;
  3843.     } else {
  3844.         help_ptr=HELPFILE;
  3845.     }
  3846. #endif /* ATARI */
  3847.  
  3848.     /* Since MSDOS DGROUP segment is being overflowed we can not allow such  */
  3849.     /* huge static variables (1k each). Instead we dynamically allocate them */
  3850.     /* on the first call to this function...                     */
  3851.     if (helpbuf == NULL) {
  3852.     helpbuf = alloc((unsigned long)MAX_LINE_LEN, "help buffer");
  3853.     prompt = alloc((unsigned long)MAX_LINE_LEN, "help prompt");
  3854.     helpbuf[0] = prompt[0] = 0;
  3855.     }
  3856.     len = base = strlen(helpbuf);
  3857.  
  3858.     /* find the end of the help command */
  3859.     for (start = c_token; !(END_OF_COMMAND); c_token++);
  3860.     /* copy new help input into helpbuf */
  3861.     if (len > 0)
  3862.     helpbuf[len++] = ' ';    /* add a space */
  3863.     capture(helpbuf + len, start, c_token - 1);
  3864.     squash_spaces(helpbuf + base);    /* only bother with new stuff */
  3865.     lower_case(helpbuf + base);    /* only bother with new stuff */
  3866.     len = strlen(helpbuf);
  3867.  
  3868.     /* now, a lone ? will print subtopics only */
  3869.     if (strcmp(helpbuf + (base ? base + 1 : 0), "?") == 0) {
  3870.     /* subtopics only */
  3871.     subtopics = 1;
  3872.     only = TRUE;
  3873.     helpbuf[base] = '\0';    /* cut off question mark */
  3874.     } else {
  3875.     /* normal help request */
  3876.     subtopics = 0;
  3877.     only = FALSE;
  3878.     }
  3879.  
  3880.     switch (help(helpbuf, help_ptr, &subtopics)) {
  3881.     case H_FOUND:{
  3882.         /* already printed the help info */
  3883.         /* subtopics now is true if there were any subtopics */
  3884.         screen_ok = FALSE;
  3885.  
  3886.         do {
  3887.         if (subtopics && !only) {
  3888.             /* prompt for subtopic with current help string */
  3889.             if (len > 0)
  3890.             (void) sprintf(prompt, "Subtopic of %s: ", helpbuf);
  3891.             else
  3892.             (void) strcpy(prompt, "Help topic: ");
  3893.             read_line(prompt);
  3894.             num_tokens = scanner(input_line);
  3895.             c_token = 0;
  3896.             more_help = !(END_OF_COMMAND);
  3897.             if (more_help)
  3898.             /* base for next level is all of current helpbuf */
  3899.             do_help();
  3900.         } else
  3901.             more_help = FALSE;
  3902.         } while (more_help);
  3903.  
  3904.         break;
  3905.     }
  3906.     case H_NOTFOUND:{
  3907.         printf("Sorry, no help for '%s'\n", helpbuf);
  3908.         break;
  3909.     }
  3910.     case H_ERROR:{
  3911.         perror(help_ptr);
  3912.         break;
  3913.     }
  3914.     default:{            /* defensive programming */
  3915.         int_error("Impossible case in switch", NO_CARET);
  3916.         /* NOTREACHED */
  3917.     }
  3918.     }
  3919.  
  3920.     helpbuf[base] = '\0';    /* cut it off where we started */
  3921. }
  3922. #endif  /* _Windows */
  3923.  
  3924. #ifdef AMIGA_AC_5
  3925. char            strg0[256];
  3926. #endif
  3927.  
  3928. do_system()
  3929. {
  3930. #ifdef AMIGA_AC_5
  3931.     char           *parms[80];
  3932.     void            getparms();
  3933.  
  3934.     getparms(input_line + 1, parms);
  3935.     if (fexecv(parms[0], parms) < 0)
  3936. #else
  3937. #if defined(ATARI)&&defined(__GNUC__)
  3938.     /* use preloaded shell, if available */
  3939.     short           (*shell_p) (char *command);
  3940.     void           *ssp;
  3941.  
  3942.     ssp = (void *) Super(NULL);
  3943.     shell_p = *(short (**) (char *)) 0x4f6;
  3944.     Super(ssp);
  3945.  
  3946.     /* this is a bit strange, but we have to have a single if */
  3947.     if ((shell_p ? (*shell_p) (input_line + 1) : system(input_line + 1)))
  3948. #else
  3949. #ifdef _Windows
  3950.     if (winsystem(input_line + 1))
  3951. #else
  3952.     if (system(input_line + 1))
  3953. #endif
  3954. #endif
  3955. #endif
  3956.     os_error("system() failed", NO_CARET);
  3957. }
  3958.  
  3959. #ifdef AMIGA_AC_5
  3960.  
  3961. /******************************************************************************/
  3962. /* */
  3963. /* Parses the command string (for fexecv use) and  converts the first token  */
  3964. /* to lower case                                                          */
  3965. /* */
  3966. /******************************************************************************/
  3967.  
  3968. void 
  3969. getparms(command, parms)
  3970.     char           *command;
  3971.     char          **parms;
  3972. {
  3973.     register int    i = 0;    /* A bunch of indices          */
  3974.     register int    j = 0;
  3975.     register int    k = 0;
  3976.  
  3977.     while (*(command + j) != '\0') {    /* Loop on string characters   */
  3978.     parms[k++] = strg0 + i;
  3979.     while (*(command + j) == ' ')
  3980.         ++j;
  3981.     while (*(command + j) != ' ' && *(command + j) != '\0') {
  3982.         if (*(command + j) == '"')    /* Get quoted string           */
  3983.         for (*(strg0 + (i++)) = *(command + (j++));
  3984.              *(command + j) != '"';
  3985.              *(strg0 + (i++)) = *(command + (j++)));
  3986.         *(strg0 + (i++)) = *(command + (j++));
  3987.     }
  3988.     *(strg0 + (i++)) = '\0';/* NUL terminate every token   */
  3989.     }
  3990.     parms[k] = '\0';
  3991.  
  3992.     for (k = strlen(strg0) - 1; k >= 0; --k)    /* Convert to lower case       */
  3993.     *(strg0 + k) >= 'A' && *(strg0 + k) <= 'Z' ? *(strg0 + k) |= 32 : *(strg0 + k);
  3994. }
  3995.  
  3996. #endif                /* AMIGA_AC_5 */
  3997.  
  3998. #ifdef READLINE
  3999. char           *
  4000. rlgets(s, n, prompt)
  4001.     char           *s;
  4002.     int             n;
  4003.     char           *prompt;
  4004. {
  4005.     char           *readline();
  4006.     static char    *line = (char *) NULL;
  4007.  
  4008.     /* If we already have a line, first free it */
  4009.     if (line != (char *) NULL)
  4010.     free(line);
  4011.  
  4012.     line = readline((interactive) ? prompt : "");
  4013.  
  4014.     /* If it's not an EOF */
  4015.     if (line) {
  4016.     if (*line)
  4017.         add_history(line);
  4018.     strncpy(s, line, n);
  4019.     return s;
  4020.     }
  4021.     return line;
  4022. }
  4023. #endif                /* READLINE */
  4024.  
  4025. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  4026.  
  4027. #ifndef _Windows
  4028. #ifdef __TURBOC__ 
  4029. /* cgets implemented using dos functions */
  4030. /* Maurice Castro 22/5/91 */
  4031. char           *
  4032. doscgets(s)
  4033.     char           *s;
  4034. {
  4035.     long            datseg;
  4036.  
  4037.     /* protect and preserve segments - call dos to do the dirty work */
  4038.     datseg = _DS;
  4039.  
  4040.     _DX = FP_OFF(s);
  4041.     _DS = FP_SEG(s);
  4042.     _AH = 0x0A;
  4043.     geninterrupt(33);
  4044.     _DS = datseg;
  4045.  
  4046.     /* check for a carriage return and then clobber it with a null */
  4047.     if (s[s[1] + 2] == '\r')
  4048.     s[s[1] + 2] = 0;
  4049.  
  4050.     /* return the input string */
  4051.     return (&(s[2]));
  4052. }
  4053. #endif                /* __TURBOC__ */
  4054. #endif                /* !_Windows */
  4055.  
  4056. #ifdef __ZTC__
  4057. void cputs(char *s)
  4058. {
  4059.    register int i = 0;
  4060.    while (s[i] != '\0')  bdos(0x02, s[i++], NULL);
  4061. }
  4062. char *cgets(char *s)
  4063. {
  4064.    bdosx(0x0A, s, NULL);
  4065.  
  4066.    if (s[s[1]+2] == '\r')
  4067.       s[s[1]+2] = 0;
  4068.  
  4069.    /* return the input string */
  4070.    return(&(s[2]));
  4071. }
  4072. #endif   /* __ZTC__ */
  4073.  
  4074.  
  4075. read_line(prompt)
  4076.     char           *prompt;
  4077. {
  4078.     register int    i;
  4079.     int             start = 0, ilen = 0;
  4080.     TBOOLEAN         more;
  4081.     int             last;
  4082.     char           *p, *crnt_prompt = prompt;
  4083.  
  4084.     if (interactive) {        /* if interactive use console IO so CED will
  4085.                  * work */
  4086. #ifndef READLINE
  4087. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4088.     printf("%s", prompt);
  4089. #else
  4090.     cputs(prompt);
  4091. #endif
  4092. #endif                /* READLINE */
  4093.     do {
  4094.         ilen = MAX_LINE_LEN - start - 1;
  4095.         input_line[start] = ilen > 126 ? 126 : ilen;
  4096. #ifdef READLINE
  4097.         input_line[start + 2] = 0;
  4098.         (void) rlgets(&(input_line[start + 2]), ilen, crnt_prompt);
  4099. #ifdef sequent
  4100.         if ((p = index(&(input_line[start + 2]), '\r'))!=NULL)
  4101.         *p = 0;
  4102.         if ((p = index(&(input_line[start + 2]), '\n'))!=NULL)
  4103.         *p = 0;
  4104. #else
  4105.         if ((p = strchr(&(input_line[start + 2]), '\r'))!=NULL)
  4106.         *p = 0;
  4107.         if ((p = strchr(&(input_line[start + 2]), '\n'))!=NULL)
  4108.         *p = 0;
  4109. #endif
  4110.         input_line[start + 1] = strlen(&(input_line[start + 2]));
  4111. #else                /* READLINE */
  4112. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP)
  4113.         (void) gets(&(input_line[start+2]));
  4114. #else
  4115. #ifdef __TURBOC__
  4116.         (void) doscgets(&(input_line[start]));
  4117. #else                /* __TURBOC__ */
  4118.         (void) cgets(&(input_line[start]));
  4119. #endif                /* __TURBOC__ */
  4120. #endif                /* _Windows || __EMX__ || DJGPP*/
  4121.         (void) putc('\n', stderr);
  4122. #endif                /* READLINE */
  4123.         if (input_line[start + 2] == 26) {
  4124.         /* end-of-file */
  4125.         (void) putc('\n', stderr);
  4126.         input_line[start] = '\0';
  4127.         inline_num++;
  4128.         if (start > 0)    /* don't quit yet - process what we have */
  4129.             more = FALSE;
  4130.         else {
  4131.             (void) putc('\n', stderr);
  4132.             return(1); /* exit gnuplot */
  4133.             /* NOTREACHED */
  4134.         }
  4135.         } else {
  4136.         /* normal line input */
  4137.         register        i = start;
  4138.         while ((input_line[i] = input_line[i + 2]) != (char) NULL)
  4139.             i++;    /* yuck!  move everything down two characters */
  4140.  
  4141.         inline_num++;
  4142.         last = strlen(input_line) - 1;
  4143.         if (last<0) last=0;  /* stop UAE in Windows */
  4144.         if (last + 1 >= MAX_LINE_LEN)
  4145.             int_error("Input line too long", NO_CARET);
  4146.  
  4147.         if (input_line[last] == '\\') {    /* line continuation */
  4148.             start = last;
  4149.             more = TRUE;
  4150.         } else
  4151.             more = FALSE;
  4152.         }
  4153. #ifndef READLINE
  4154.         if (more)
  4155. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4156.         printf("> ");
  4157. #else
  4158.         cputs("> ");
  4159. #endif
  4160. #else
  4161.         crnt_prompt = "> ";
  4162. #endif                /* READLINE */
  4163.     } while (more);
  4164.     } else {            /* not interactive */
  4165.     if (interactive)
  4166.         fputs(prompt, stderr);
  4167.     do {
  4168.         /* grab some input */
  4169.         if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4170.         == (char *) NULL) {
  4171.         /* end-of-file */
  4172.         if (interactive)
  4173.             (void) putc('\n', stderr);
  4174.         input_line[start] = '\0';
  4175.         inline_num++;
  4176.         if (start > 0)    /* don't quit yet - process what we have */
  4177.             more = FALSE;
  4178.         else
  4179.             return(1);  /* exit gnuplot */
  4180.         } else {
  4181.         /* normal line input */
  4182.         last = strlen(input_line) - 1;
  4183.         if (input_line[last] == '\n') {    /* remove any newline */
  4184.             input_line[last] = '\0';
  4185.             /* Watch out that we don't backup beyond 0 (1-1-1) */
  4186.             if (last > 0)
  4187.             --last;
  4188.             inline_num++;
  4189.         } else if (last + 1 >= MAX_LINE_LEN)
  4190.             int_error("Input line too long", NO_CARET);
  4191.  
  4192.         if (input_line[last] == '\\') {    /* line continuation */
  4193.             start = last;
  4194.             more = TRUE;
  4195.         } else
  4196.             more = FALSE;
  4197.         }
  4198.         if (more && interactive)
  4199.         fputs("> ", stderr);
  4200.     } while (more);
  4201.     }
  4202.     return(0);
  4203. }
  4204.  
  4205.  
  4206. do_shell()
  4207. {
  4208.     register char  *comspec;
  4209.     if ((comspec = getenv("COMSPEC")) == (char *) NULL)
  4210.     comspec = "\\command.com";
  4211. #ifdef _Windows
  4212.     if (WinExec(comspec, SW_SHOWNORMAL) <= 32)
  4213. #else
  4214. #ifdef DJGPP
  4215.     if (system(comspec) == -1)
  4216. #else
  4217.     if (spawnl(P_WAIT, comspec, NULL) == -1)
  4218. #endif
  4219. #endif
  4220.     os_error("unable to spawn shell", NO_CARET);
  4221. }
  4222.  
  4223. #else                /* MSDOS */
  4224. /* plain old Unix */
  4225.  
  4226. read_line(prompt)
  4227.     char           *prompt;
  4228. {
  4229.     int             start = 0;
  4230.     TBOOLEAN         more = FALSE;
  4231.     int             last = 0;
  4232.  
  4233. #ifndef READLINE
  4234.     if (interactive)
  4235.     fputs(prompt, stderr);
  4236. #endif                /* READLINE */
  4237.     do {
  4238.     /* grab some input */
  4239. #ifdef READLINE
  4240.     if (((interactive)
  4241.          ? rlgets(&(input_line[start]), MAX_LINE_LEN - start,
  4242.               ((more) ? "> " : prompt))
  4243.          : fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin))
  4244.         == (char *) NULL) {
  4245. #else
  4246.     if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4247.         == (char *) NULL) {
  4248. #endif                /* READLINE */
  4249.         /* end-of-file */
  4250.         if (interactive)
  4251.         (void) putc('\n', stderr);
  4252.         input_line[start] = '\0';
  4253.         inline_num++;
  4254.         if (start > 0)    /* don't quit yet - process what we have */
  4255.         more = FALSE;
  4256.         else
  4257.         return(1); /* exit gnuplot */
  4258.     } else {
  4259.         /* normal line input */
  4260.         last = strlen(input_line) - 1;
  4261.         if (input_line[last] == '\n') {    /* remove any newline */
  4262.         input_line[last] = '\0';
  4263.         /* Watch out that we don't backup beyond 0 (1-1-1) */
  4264.         if (last > 0)
  4265.             --last;
  4266.         } else if (last + 1 >= MAX_LINE_LEN)
  4267.         int_error("Input line too long", NO_CARET);
  4268.  
  4269.         if (input_line[last] == '\\') {    /* line continuation */
  4270.         start = last;
  4271.         more = TRUE;
  4272.         } else
  4273.         more = FALSE;
  4274.     }
  4275. #ifndef READLINE
  4276.     if (more && interactive)
  4277.         fputs("> ", stderr);
  4278. #endif
  4279.     } while (more);
  4280.     return(0);
  4281. }
  4282.  
  4283. #ifdef VFORK
  4284.  
  4285. do_shell()
  4286. {
  4287.     register char  *shell;
  4288.     register int    p;
  4289.     static int      execstat;
  4290.     if (!(shell = getenv("SHELL")))
  4291.     shell = SHELL;
  4292. #ifdef AMIGA_AC_5
  4293.     execstat = fexecl(shell, shell, NULL);
  4294. #else
  4295.     if ((p = vfork()) == 0) {
  4296.     execstat = execl(shell, shell, NULL);
  4297.     _exit(1);
  4298.     } else if (p == -1)
  4299.     os_error("vfork failed", c_token);
  4300.     else
  4301.     while (wait(NULL) != p)
  4302. #endif
  4303.         ;
  4304.     if (execstat == -1)
  4305.     os_error("shell exec failed", c_token);
  4306.     (void) putc('\n', stderr);
  4307. }
  4308. #else                /* VFORK */
  4309.  
  4310. #ifdef AMIGA_SC_6_1
  4311. do_shell()
  4312. {
  4313.     register char  *shell;
  4314.     if (!(shell = getenv("SHELL")))
  4315.     shell = SHELL;
  4316.  
  4317.     if (system(shell))
  4318.     os_error("system() failed", NO_CARET);
  4319.  
  4320.     (void) putc('\n', stderr);
  4321. }
  4322. #else                /* AMIGA_SC_6_1 */
  4323. #ifdef OS2
  4324. do_shell()
  4325. {
  4326.     register char  *shell;
  4327.     if (!(shell = getenv("COMSPEC")))
  4328.     shell = SHELL;
  4329.  
  4330.     if (system(shell) == -1 )
  4331.     os_error("system() failed", NO_CARET);
  4332.  
  4333.     (void) putc('\n', stderr);
  4334. }
  4335. #else                           /* ! OS2 */
  4336. #define EXEC "exec "
  4337. do_shell()
  4338. {
  4339.     static char     exec[100] = EXEC;
  4340.     register char  *shell;
  4341.     if (!(shell = getenv("SHELL")))
  4342.     shell = SHELL;
  4343.  
  4344.     if (system(strncpy(&exec[sizeof(EXEC) - 1], shell,
  4345.                sizeof(exec) - sizeof(EXEC) - 1)))
  4346.     os_error("system() failed", NO_CARET);
  4347.  
  4348.     (void) putc('\n', stderr);
  4349. }
  4350. #endif                          /* OS2 */
  4351. #endif                /* AMIGA_SC_6_1 */
  4352. #endif                /* VFORK */
  4353. #endif                /* MSDOS */
  4354. #endif                /* vms */
  4355.  
  4356. #ifdef _Windows
  4357. /* there is a system like call on MS Windows but it is a bit difficult to 
  4358.    use, so we will invoke the command interpreter and use it to execute the 
  4359.    commands */
  4360. int winsystem(s)
  4361. char *s;
  4362. {
  4363.     LPSTR comspec;
  4364.     LPSTR execstr;
  4365.     LPSTR p;
  4366.  
  4367.     /* get COMSPEC environment variable */
  4368. #ifdef WIN32
  4369.     char envbuf[81];
  4370.     GetEnvironmentVariable("COMSPEC", envbuf, 80);
  4371.     if (*envbuf == '\0')
  4372.         comspec = "\\command.com";
  4373.     else
  4374.         comspec = envbuf;
  4375. #else
  4376.     p = GetDOSEnvironment();
  4377.     comspec = "\\command.com";
  4378.     while (*p) {
  4379.         if (!strncmp(p,"COMSPEC=",8)) {
  4380.             comspec=p+8;
  4381.             break;
  4382.         }
  4383.         p+=strlen(p)+1;
  4384.     }
  4385. #endif
  4386.     /* if the command is blank we must use command.com */
  4387.     p = s;
  4388.     while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
  4389.         p++;
  4390.     if (*p == '\0')
  4391.     {
  4392.         WinExec(comspec, SW_SHOWNORMAL);
  4393.         }
  4394.     else
  4395.     {
  4396.         /* attempt to run the windows/dos program via windows */
  4397.         if (WinExec(s, SW_SHOWNORMAL) <= 32)
  4398.         {
  4399.             /* attempt to run it as a dos program from command line */
  4400.             execstr = (char *) malloc(strlen(s) + strlen(comspec) + 6);
  4401.             strcpy(execstr, comspec);
  4402.             strcat(execstr, " /c ");
  4403.             strcat(execstr, s);
  4404.             WinExec(execstr, SW_SHOWNORMAL);
  4405.             free(execstr);
  4406.             }
  4407.         }
  4408.  
  4409.     /* regardless of the reality return OK - the consequences of */
  4410.     /* failure include shutting down Windows */
  4411.     return(0);        /* success */
  4412.     }
  4413. #endif
  4414.