home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Gnuplot 3.5 / source / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-11  |  116.3 KB  |  4,431 lines  |  [TEXT/R*ch]

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