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

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