1 /* NIGHTFALL Light Curve Synthesis Program                                 */
2 /* Copyright (C) 1998 Rainer Wichmann                                      */
3 /*                                                                         */
4 /*  This program is free software; you can redistribute it                 */
5 /*  and/or modify                                                          */
6 /*  it under the terms of the GNU General Public License as                */
7 /*  published by                                                           */
8 /*  the Free Software Foundation; either version 2 of the License, or      */
9 /*  (at your option) any later version.                                    */
10 /*                                                                         */
11 /*  This program is distributed in the hope that it will be useful,        */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
14 /*  GNU General Public License for more details.                           */
15 /*                                                                         */
16 /*  You should have received a copy of the GNU General Public License      */
17 /*  along with this program; if not, write to the Free Software            */
18 /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
19 
20 
21 /*
22  +  this is a library of functions that somehow emulate the
23  +    PGPLOT calls in 'nightfall'. Some of the emulated
24  +    functions are just dirty hacks. Display of images
25  +    is not possible with GNUPLOT
26  +
27  +  the different plotting 'philosophy' of GNUPLOT and PGPLOT
28  +    adds considerable complications
29  +
30  +  based on demo code in the gnuplot faq
31  +    for calling gnuplot from C using named pipes
32  +    pipes are opened in /tmp directory
33  +
34  +  animated mode requires three separate data pipes to run
35  +
36  +  for details about the arguments of these subroutines, see
37  +    the PGPLOT manual
38 */
39 
40 /* ==========================================================
41  *
42  * 'set no...' is deprecated syntax, new syntax is 'unset ...'
43  *
44  * ==========================================================
45  */
46 
47 
48 
49 #include <stdlib.h>
50 
51 #include <sys/stat.h>
52 #ifdef HAVE_SYS_TIMERS_H
53 #include <sys/timers.h>
54 #endif
55 #include <signal.h>
56 #include <string.h>
57 #include <stdio.h>
58 #include <float.h>
59 
60 #include "Light.h"
61 
62 #ifdef HAVE_UNISTD_H
63 #include <sys/types.h>
64 #include <unistd.h>
65 #endif
66 #include <time.h>
67 
68 
69 #ifdef _WITH_GNUPLOT
70 
71 static  struct timespec sleep_req = { 0, 20000000 };
72 static  struct timespec sleep_rem;
73 
74 static  int   gnuSubpageflag = OFF;    /* flag for multiplot                */
75 static  int   gnuPageX = 0;            /* # of pages in x (multiplot)       */
76 static  int   gnuPageY = 0;            /* # of pages in y (multiplot)       */
77 static  int   gnuPageXn = 0;           /* actual page in x (multiplot)      */
78 static  int   gnuPageYn = 0;           /* actual page in y (multiplot)      */
79 static  float gnuLw = 1.;              /* linewidth                         */
80 static  float gnuPs = 1.;              /* pointsize                         */
81 static  int   gnuLt = 1;               /* linetype                          */
82 static  float gnuOrig_x, gnuOrig_y;    /* origin location                   */
83 static  float gnuSizex, gnuSizey;      /* page size                         */
84 static  float gnuMOrig_x, gnuMOrig_y;  /* origin location / bookkeping      */
85 static  float gnuMSizex, gnuMSizey;    /* page size  / bookkeping           */
86 static  char  gnuCommands[1024];       /* command pipe                      */
87 static  char  gnuData1[1024];          /* data    pipe1                     */
88 static  char  gnuData2[1024];          /* data    pipe2                     */
89 static  char  gnuData3[1024];          /* data    pipe3                     */
90 static  char  gnuData4[1024];          /* data    pipe4                     */
91 static  FILE  *gnustr, *gnudat1, *gnudat2;  /* pipe filehandles             */
92 static  FILE  *gnudat3, *gnudat4;           /* pipe filehandles             */
93 static  pid_t childPid;                 /* PID of forked process            */
94 static  pid_t processGroup;             /* PID of forked process            */
95 static  char  sig_msg[64];              /* signal name                      */
96 
97 /******************************************************************
98  @package   nightfall
99  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
100  @version   1.0
101  @short     Return signal name
102  @param     (void)
103  @return    (void)
104  @heading   Signal Handling
105 *******************************************************************/
nf_signame(int signum)106 char * nf_signame(int signum)
107 {
108 
109   switch (signum)
110     {
111 #ifdef SIGHUP
112     case SIGHUP: return "Hangup";
113 #endif
114 #ifdef SIGINT
115     case SIGINT: return "Interrupt";
116 #endif
117 #ifdef SIGQUIT
118     case SIGQUIT: return "Quit";
119 #endif
120 #ifdef SIGILL
121     case SIGILL: return "Illegal instruction";
122 #endif
123 #ifdef SIGTRAP
124     case SIGTRAP: return "Trace/breakpoint trap";
125 #endif
126 #ifdef SIGABRT
127     case SIGABRT: return "IOT trap/Abort";
128 #endif
129 #ifdef SIGBUS
130     case SIGBUS: return "Bus error";
131 #endif
132 #ifdef SIGFPE
133     case SIGFPE: return "Floating point exception";
134 #endif
135 #ifdef SIGKILL
136     case SIGKILL: return "Killed";
137 #endif
138 #ifdef SIGUSR1
139     case SIGUSR1: return "User defined signal 1";
140 #endif
141 #ifdef SIGSEGV
142     case SIGSEGV: return "Segmentation fault";
143 #endif
144 #ifdef SIGUSR2
145     case SIGUSR2: return "User defined signal 2";
146 #endif
147 #ifdef SIGPIPE
148     case SIGPIPE: return "Broken pipe";
149 #endif
150 #ifdef SIGALRM
151     case SIGALRM: return "Alarm clock";
152 #endif
153 #ifdef SIGTERM
154     case SIGTERM: return "Terminated";
155 #endif
156 #ifdef SIGSTKFLT
157     case SIGSTKFLT: return "Stack fault";
158 #endif
159 #ifdef SIGCHLD
160     case SIGCHLD: return "Child exited";
161 #endif
162 #ifdef SIGCONT
163     case SIGCONT: return "Continued";
164 #endif
165 #ifdef SIGSTOP
166     case SIGSTOP: return "Stopped (signal)";
167 #endif
168 #ifdef SIGTSTP
169     case SIGTSTP: return "Stopped";
170 #endif
171 #ifdef SIGTTIN
172     case SIGTTIN: return "Stopped (tty input)";
173 #endif
174 #ifdef SIGTTOU
175     case SIGTTOU: return "Stopped (tty output)";
176 #endif
177 #ifdef SIGURG
178     case SIGURG: return "Urgent condition";
179 #endif
180 #ifdef SIGXCPU
181     case SIGXCPU: return "CPU time limit exceeded";
182 #endif
183 #ifdef SIGXFSZ
184     case SIGXFSZ: return "File size limit exceeded";
185 #endif
186 #ifdef SIGVTALRM
187     case SIGVTALRM: return "Virtual time alarm";
188 #endif
189 #ifdef SIGPROF
190     case SIGPROF: return "Profile signal";
191 #endif
192 #ifdef SIGWINCH
193     case SIGWINCH: return "Window size changed";
194 #endif
195 #ifdef SIGIO
196     case SIGIO: return "Possible I/O";
197 #endif
198 #ifdef SIGPWR
199     case SIGPWR: return "Power failure";
200 #endif
201 #ifdef SIGUNUSED
202     case SIGUNUSED: return "Unused signal";
203 #endif
204     }
205   sprintf (sig_msg, "unknown signal (%8d)", signum);
206   return sig_msg;
207 }
208 
209 /******************************************************************
210  @package   nightfall
211  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
212  @version   1.0
213  @short     Handle signals, cleanup FIFO's on abnormal termination
214  @param     (void)
215  @return    (void)
216  @heading   Signal Handling
217 *******************************************************************/
gnu_sighandler(int signal)218 void gnu_sighandler (int signal)
219 {
220 
221   if (Flags.plotOpened == ON) {
222     Flags.plotOpened = OFF;
223     cpgend();
224   }
225 
226 #ifdef HAVE_GNOME
227   if (Flags.interactive == ON)
228     doQuit();
229 #endif
230 
231   /*  is it safe to use fprintf() in a signal handler ?
232   fprintf(stderr,"** SIGHANDLE **: caught signal: %s ...\n",
233 	  nf_signame(signal));
234   fprintf(stderr,"                 ... cleanup and exit to system \n");
235    */
236 
237   fputs("** SIGHANDLE **: caught signal: ", stderr);
238   fputs(nf_signame(signal), stderr);
239   fputs("\n", stderr);
240 
241   exit(EXIT_FAILURE);
242 }
243 
244 /******************************************************************
245  @package   nightfall
246  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
247  @version   1.0
248  @short     Additional function required at plot start
249  @param     (void)
250  @return    (void)
251  @heading   PGPLOT emulation
252 *******************************************************************/
gnu_end()253 void gnu_end()
254 {
255    fprintf(gnustr, "unset multiplot\n");
256    fflush(gnustr);
257 
258    return;
259 }
260 
261 /******************************************************************
262  @package   nightfall
263  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
264  @version   1.0
265  @short     Additional function required at temorary plot end
266  @param     (void)
267  @return    (void)
268  @heading   PGPLOT emulation
269 *******************************************************************/
gnu_start()270 void gnu_start()
271 {
272   fprintf(gnustr, "set multiplot\n");
273   fflush(gnustr);
274   return;
275 }
276 
277 
278 /******************************************************************
279  @package   nightfall
280  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
281  @version   1.0
282  @short     Image display
283  @param     (see PGPLOT manual)
284  @return    (void)
285  @heading   PGPLOT emulation
286 *******************************************************************/
cpgimag(const float * a,int idim,int jdim,int i1,int i2,int j1,int j2,float a1,float a2,const float * tr)287 void cpgimag(const float *a, int idim, int jdim, int i1, int i2, int j1,
288 int j2, float a1, float a2, const float *tr)
289 {
290   /* there is no such resource in GNUPLOT */
291 
292   return;
293 }
294 
295 /******************************************************************
296  @package   nightfall
297  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
298  @version   1.0
299  @short     Contour plotting
300  @param     (see PGPLOT manual)
301  @return    (void)
302  @heading   PGPLOT emulation
303 *******************************************************************/
cpgcont(const float * a,int xdim,int ydim,int x1,int x2,int y1,int y2,const float * c,int nc,const float * tr)304 void cpgcont(const float *a, int xdim, int ydim, int x1, int x2, int y1,
305 int y2, const float *c, int nc, const float *tr)
306 {
307    register  unsigned int i, j;     /*  loop variables            */
308    register  unsigned int l;        /*  loop variables            */
309    float     xfactor = 60.0;        /* scale factor x             */
310    float     yfactor = 60.0;        /* scale factor y             */
311    float     xstart  = -1.0;        /* start x                    */
312    float     ystart  = -1.0;        /* start y                    */
313 
314 
315    if (xdim == _CHI_SCANS_) {
316      xfactor = 1.0/tr[1];
317      yfactor = 1.0/tr[5];
318      xstart  = tr[0] + 0.5*tr[1];
319      ystart  = tr[3] + 0.5*tr[5];
320    }
321 
322    fprintf(gnustr,"unset surface\n");
323    fprintf(gnustr,"set contour\n");
324    fprintf(gnustr,"set view 0., 0., 1.5\n");
325 
326    /* put commands in command pipe  and data in data pipe         */
327 
328    fprintf(gnustr,"set cntrparam cubicspline\n");
329    fprintf(gnustr,"set cntrparam levels discrete %f, %f, %f, %f, %f\n",
330           *(c),
331           *(c + 1),
332           *(c + 2),
333           *(c + 3),
334           *(c + 4));
335    fprintf(gnustr,"clear\n");
336    fprintf(gnustr,"splot '%s'  index 0 notitle w lines \n", gnuData4);
337    fflush(gnustr);
338 
339    /* this fopen must occur AFTER gnuplot has already             */
340    /*    tried to open the pipe for reading                       */
341 
342    gnudat4 = fopen(gnuData4, "w");
343    if(!gnudat4) perror (_("could not open pipe"));
344 
345    l = (xdim/10)*10;
346 
347      for (i=0; i < ydim; ++i) {
348 
349        /* some dynamic loop unrolling                             */
350 
351        j= 0;
352 
353        while (j < l) {
354 
355          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
356                j/xfactor + xstart,     i/yfactor + ystart,
357 		 MAX(-99., *(a + xdim*i + j)) );
358          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
359                (1+j)/xfactor + xstart, i/yfactor + ystart,
360 		 MAX(-99., *(a + xdim*i + j + 1)) );
361          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
362                (2+j)/xfactor + xstart, i/yfactor + ystart,
363 		 MAX(-99., *(a + xdim*i + j + 2)) );
364          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
365                (3+j)/xfactor + xstart, i/yfactor + ystart,
366 		 MAX(-99., *(a + xdim*i + j + 3)) );
367          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
368                (4+j)/xfactor + xstart, i/yfactor + ystart,
369 		 MAX(-99., *(a + xdim*i + j + 4))  );
370          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
371                (5+j)/xfactor + xstart, i/yfactor + ystart,
372 		 MAX(-99., *(a + xdim*i + j + 5)) );
373          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
374                (6+j)/xfactor + xstart, i/yfactor + ystart,
375 		 MAX(-99., *(a + xdim*i + j + 6)) );
376          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
377                (7+j)/xfactor + xstart, i/yfactor + ystart,
378 		 MAX(-99., *(a + xdim*i + j + 7)) );
379          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
380                (8+j)/xfactor + xstart, i/yfactor + ystart,
381 		 MAX(-99., *(a + xdim*i + j + 8)) );
382          fprintf(gnudat4,"%7.4f %7.4f %7.4f\n",
383                (9+j)/xfactor + xstart, i/yfactor + ystart,
384 		 MAX(-99., *(a + xdim*i + j + 9)) );
385 
386 	 j = j + 10;
387        }
388 
389        if (j < xdim) {
390 
391 	 switch (xdim -j)
392 	   {
393 	   case 9:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
394 			    i/yfactor + ystart,
395 			    MAX(-99., *(a + xdim*i + j))); ++j;
396 	   case 8:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
397 			    i/yfactor + ystart,
398 			    MAX(-99., *(a + xdim*i + j))); ++j;
399 	   case 7:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
400 			    i/yfactor + ystart,
401 			    MAX(-99., *(a + xdim*i + j))); ++j;
402 	   case 6:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
403 			    i/yfactor + ystart,
404 			    MAX(-99., *(a + xdim*i + j))); ++j;
405 	   case 5:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
406 			    i/yfactor + ystart,
407 			    MAX(-99., *(a + xdim*i + j))); ++j;
408 	   case 4:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
409 			    i/yfactor + ystart,
410 			    MAX(-99., *(a + xdim*i + j))); ++j;
411 	   case 3:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
412 			    i/yfactor + ystart,
413 			    MAX(-99., *(a + xdim*i + j))); ++j;
414 	   case 2:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
415 			    i/yfactor + ystart,
416 			    MAX(-99., *(a + xdim*i + j))); ++j;
417 	   case 1:  fprintf(gnudat4,"%7.4f %7.4f %7.4f\n", j/xfactor + xstart,
418 			    i/yfactor + ystart,
419 			    MAX(-99., *(a + xdim*i + j))); ++j;
420 	   default: break;
421 	   }
422        }
423 
424        fprintf(gnudat4," \n");
425 
426      }
427 
428      fclose(gnudat4);
429 
430      /* to avoid 'broken pipe' errors                                */
431      nanosleep (&sleep_req, &sleep_rem);
432 
433      return;
434 }
435 
436 
437 /******************************************************************
438  @package   nightfall
439  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
440  @version   1.0
441  @short     Image display - wedge
442  @param     (see PGPLOT manual)
443  @return    (void)
444  @heading   PGPLOT emulation
445 *******************************************************************/
cpgwedg(const char * side,float disp,float width,float fg,float bg,const char * label)446 void cpgwedg(const char *side, float disp, float width, float fg, float bg,
447 const char *label)
448 {
449   /* there is no such resource in GNUPLOT */
450 
451    return;
452 }
453 
454 /******************************************************************
455  @package   nightfall
456  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
457  @version   1.0
458  @short     Set line size
459  @param     (see PGPLOT manual)
460  @return    (void)
461  @heading   PGPLOT emulation
462 *******************************************************************/
cpgsls(int fnt)463 void cpgsls(int fnt)
464 {
465    /* not required                         */
466    return;
467 }
468 
469 /******************************************************************
470  @package   nightfall
471  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
472  @version   1.0
473  @short     Set character size
474  @param     (see PGPLOT manual)
475  @return    (void)
476  @heading   PGPLOT emulation
477 *******************************************************************/
cpgscf(int fnt)478 void cpgscf(int fnt)
479 {
480    /* not required                         */
481    return;
482 }
483 
484 /******************************************************************
485  @package   nightfall
486  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
487  @version   1.0
488  @short     Viewport size default
489  @param     (see PGPLOT manual)
490  @return    (void)
491  @heading   PGPLOT emulation
492 *******************************************************************/
cpgvstd()493 void cpgvstd()
494 {
495   cpgsvp(0.1, 0.9, 0.1, 0.9);
496   return;
497 }
498 
499 /******************************************************************
500  @package   nightfall
501  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
502  @version   1.0
503  @short     Text label
504  @param     (see PGPLOT manual)
505  @return    (void)
506  @heading   PGPLOT emulation
507 *******************************************************************/
cpgtext(float xpos,float ypos,const char * label)508 void cpgtext(float xpos, float ypos, const char *label)
509 {
510   fprintf(gnustr,"set label '%s' at first %f, %f \n", label, xpos, ypos );
511   fflush(gnustr);
512   return;
513 }
514 
515 /******************************************************************
516  @package   nightfall
517  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
518  @version   1.0
519  @short     Set title and axis labels
520  @param     (see PGPLOT manual)
521  @return    (void)
522  @heading   PGPLOT emulation
523 *******************************************************************/
cpglab(const char * xlbl,const char * ylbl,const char * toplbl)524 void cpglab(const char *xlbl, const char *ylbl, const char *toplbl)
525 {
526     fprintf(gnustr,"set title '%s'  \n", toplbl);
527     fprintf(gnustr,"set xlabel '%s'  \n", xlbl);
528     fprintf(gnustr,"set ylabel '%s'  \n", ylbl);
529     fflush(gnustr);
530     return;
531 }
532 
533 /******************************************************************
534  @package   nightfall
535  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
536  @version   1.0
537  @short     Draw a line
538  @param     (see PGPLOT manual)
539  @return    (void)
540  @heading   PGPLOT emulation
541 *******************************************************************/
cpgline(int n,const float * xpts,const float * ypts)542 void cpgline(int n,  const float *xpts, const float *ypts)
543 {
544     int i;                    /* loop variable                    */
545 
546     /* put commands in command pipe  and data in data pipe        */
547 
548     fprintf(gnustr,"plot '%s' notitle w lines \n", gnuData1);
549     fflush(gnustr);
550 
551     /* this fopen must occur AFTER gnuplot has already            */
552     /*    tried to open the pipe for reading                      */
553 
554     gnudat1 = fopen(gnuData1, "w");
555     if(!gnudat1) perror (_("could not open pipe"));
556     for(i = 0; i < n; ++i) {
557       fprintf(gnudat1,"%8.5g %8.5g\n", xpts[i], ypts[i]);
558     }
559 
560     fclose(gnudat1);
561     return;
562 }
563 
564 /******************************************************************
565  @package   nightfall
566  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
567  @version   1.0
568  @short     Plot points
569  @param     (see PGPLOT manual)
570  @return    (void)
571  @heading   PGPLOT emulation
572 *******************************************************************/
cpgpt(int n,const float * xpts,const float * ypts,int npt)573 void cpgpt(int n,  const float *xpts, const float *ypts, int npt)
574 {
575     int i;                    /* loop variable                    */
576 
577     /* put commands in command pipe  and data in data pipe        */
578 
579     fprintf(gnustr,"plot '%s' notitle w points  ps %f \n",
580 	    gnuData3, gnuPs);
581     fflush(gnustr);
582 
583     /* this fopen must occur AFTER gnuplot has already            */
584     /*    tried to open the pipe for reading                      */
585 
586     gnudat3 = fopen(gnuData3, "w");
587     if(!gnudat3) perror (_("could not open pipe"));
588     for(i = 0; i < n; ++i)
589       fprintf(gnudat3,"%8.5g %8.5g\n", xpts[i], ypts[i]);
590 
591     fclose(gnudat3);
592     return;
593 }
594 
595 /******************************************************************
596  @package   nightfall
597  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
598  @version   1.0
599  @short     Plot points (two datasets)
600  @tip       not in PGPLOT
601  @param     (see PGPLOT manual)
602  @return    (void)
603  @heading   PGPLOT emulation
604 *******************************************************************/
cpgpt2(int n,int n2,const float * x1pts,const float * y1pts,const float * x2pts,const float * y2pts)605 void cpgpt2(int n, int n2,  const float *x1pts, const float *y1pts,
606 const float *x2pts, const float *y2pts)
607 {
608     int i;                    /* loop variable                    */
609 
610     /* put commands in command pipe  and data in data pipe        */
611 
612     fprintf(gnustr,
613       "plot '%s' notitle w points pt 0, '%s' notitle w points pt 0\n",
614        gnuData1, gnuData2);
615     fflush(gnustr);
616 
617     /* this fopen must occur AFTER gnuplot has already            */
618     /*    tried to open the pipe for reading                      */
619 
620     gnudat1 = fopen(gnuData1, "w");
621     if(!gnudat1) perror (_("could not open pipe"));
622 
623     for(i = 0; i < n; ++i) {
624       fprintf(gnudat1,"%8.5g %8.5g\n", x1pts[i], y1pts[i]);
625     }
626     fclose(gnudat1);
627 
628     gnudat2 = fopen(gnuData2, "w");
629     if(!gnudat2) perror (_("could not open pipe"));
630 
631     for(i = 0; i < n2; ++i) {
632       fprintf(gnudat2,"%8.5g %8.5g\n", x2pts[i], y2pts[i]);
633     }
634 
635     fclose(gnudat2);
636     return;
637 }
638 
639 /******************************************************************
640  @package   nightfall
641  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
642  @version   1.0
643  @short     Draw lines (two datasets)
644  @tip       not in PGPLOT
645  @param     (see PGPLOT manual)
646  @return    (void)
647  @heading   PGPLOT emulation
648 *******************************************************************/
cpgline2(int n,int n2,const float * x1pts,const float * y1pts,const float * x2pts,const float * y2pts)649 void cpgline2(int n, int n2, const float *x1pts, const float *y1pts,
650 const float *x2pts, const float *y2pts)
651 {
652     int i;                    /* loop variable                    */
653 
654     /* put commands in command pipe  and data in data pipe        */
655 
656     fprintf(gnustr,
657       "plot '%s' index 0:1 notitle w lines lt 2\n",
658        gnuData2);
659     fflush(gnustr);
660 
661     /* this fopen must occur AFTER gnuplot has already            */
662     /*    tried to open the pipe for reading                      */
663 
664     gnudat2 = fopen(gnuData2, "w");
665     if(!gnudat2) perror (_("could not open pipe"));
666 
667     for(i = 0; i < n; ++i) {
668       fprintf(gnudat2,"%8.5g %8.5g\n", x1pts[i], y1pts[i]);
669     }
670     fprintf(gnudat2," \n");
671     fprintf(gnudat2," \n");
672     for(i = 0; i < n2; ++i) {
673       fprintf(gnudat2,"%8.5g %8.5g\n", x2pts[i], y2pts[i]);
674     }
675 
676     fclose(gnudat2);
677     return;
678 }
679 
680 /******************************************************************
681  @package   nightfall
682  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
683  @version   1.0
684  @short     Draw points + line (three datasets)
685  @tip       not in PGPLOT
686  @param     (see PGPLOT manual)
687  @return    (void)
688  @heading   PGPLOT emulation
689 *******************************************************************/
cpgline2pt(int n,int n2,int n3,const float * x1pts,const float * y1pts,const float * x2pts,const float * y2pts,const float * x3pts,const float * y3pts)690 void cpgline2pt(int n, int n2, int n3, const float *x1pts, const float *y1pts,
691                 const float *x2pts, const float *y2pts,
692                 const float *x3pts, const float *y3pts)
693 {
694     int i;                    /* loop variable                    */
695 
696     /* put commands in command pipe  and data in data pipe        */
697 
698     /* -------------   POINTS FIRST, LINES SECOND --------------- */
699 
700     fprintf(gnustr,
701 	    "plot '%s' notitle w points  ps %f, '%s' index 0:1 notitle w lines lt 2\n",
702 	    gnuData1, gnuPs, gnuData2);
703     fflush(gnustr);
704 
705     /* this fopen must occur AFTER gnuplot has already            */
706     /*    tried to open the pipe for reading                      */
707 
708     gnudat1 = fopen(gnuData1, "w");
709     for(i = 0; i < n3; ++i) {
710       fprintf(gnudat1,"%8.5g %8.5g\n", x3pts[i], y3pts[i]);
711     }
712     fclose(gnudat1);
713 
714     gnudat2 = fopen(gnuData2, "w");
715     if(!gnudat2) perror (_("could not open pipe"));
716 
717     for(i = 0; i < n; ++i) {
718       fprintf(gnudat2,"%8.5g %8.5g\n", x1pts[i], y1pts[i]);
719     }
720     fprintf(gnudat2," \n");
721     fprintf(gnudat2," \n");
722     for(i = 0; i < n2; ++i) {
723       fprintf(gnudat2,"%8.5g %8.5g\n", x2pts[i], y2pts[i]);
724     }
725     fclose(gnudat2);
726     return;
727 
728 }
729 
730 /******************************************************************
731  @package   nightfall
732  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
733  @version   1.0
734  @short     Draw points + line (two datasets)
735  @tip       not in PGPLOT
736  @param     (see PGPLOT manual)
737  @return    (void)
738  @heading   PGPLOT emulation
739 *******************************************************************/
cpglinept(int n,int n2,const float * x1pts,const float * y1pts,const float * x2pts,const float * y2pts)740 void cpglinept(int n,  int n2, const float *x1pts, const float *y1pts,
741 const float *x2pts, const float *y2pts)
742 {
743     int i;                    /* loop variable                    */
744 
745     /* put commands in command pipe  and data in data pipe        */
746 
747     fprintf(gnustr,
748       "plot '%s' notitle w lines, '%s' notitle w points ps %f\n",
749        gnuData1, gnuData2, gnuPs);
750     fflush(gnustr);
751 
752     /* this fopen must occur AFTER gnuplot has already            */
753     /*    tried to open the pipe for reading                      */
754 
755     gnudat1 = fopen(gnuData1, "w");
756     if(!gnudat1) perror (_("could not open pipe"));
757 
758     for(i = 0; i < n; ++i) {
759       fprintf(gnudat1,"%f %f\n", x1pts[i], y1pts[i]);
760     }
761     fclose(gnudat1);
762 
763     gnudat2 = fopen(gnuData2, "w");
764     if(!gnudat2) perror (_("could not open pipe"));
765 
766     for(i = 0; i < n2; ++i) {
767       fprintf(gnudat2,"%f %f\n", x2pts[i], y2pts[i]);
768     }
769     fclose(gnudat2);
770     return;
771 }
772 
773 /******************************************************************
774  @package   nightfall
775  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
776  @version   1.0
777  @short     Set box type
778  @tip       only BCST, BCMST, ABCNST supported
779  @param     (see PGPLOT manual)
780  @return    (void)
781  @heading   PGPLOT emulation
782 *******************************************************************/
cpgbox(const char * xopt,float xtick,int nxsub,const char * yopt,float ytick,int nysub)783 void cpgbox(const char *xopt, float xtick, int nxsub,
784 	    const char *yopt, float ytick, int nysub)
785 {
786 
787   /* set tickmarks                                                */
788 
789   if (xtick != 0.0) fprintf(gnustr, "set xtics %10.2f \n", xtick);
790   if (strcmp("BCMST", yopt) != 0)
791     if (ytick != 0.0) fprintf(gnustr, "set ytics %10.2f \n", ytick);
792 
793   /* required: support for BCST, BCMST, ABCNST                    */
794 
795   if (strcmp("BCST", xopt) == 0) {
796     fprintf(gnustr, "set xtics (\"\" 0)\n");
797   }
798   if (strcmp("BCST", yopt) == 0) {
799     fprintf(gnustr, "set ytics (\"\" 0)\n");
800   }
801 
802   if (strcmp("BCNST", xopt) == 0) {
803     fprintf(gnustr, "set xtics \n");
804     if (xtick != 0.0) fprintf(gnustr, "set xtics %10.5g \n", xtick);
805   }
806   if (strcmp("BCNST", yopt) == 0) {
807     fprintf(gnustr, "set ytics \n");
808     if (ytick != 0.0) fprintf(gnustr, "set ytics %10.5g \n", ytick);
809   }
810 
811   if (strcmp("BCMST", xopt) == 0) {
812     fprintf(gnustr, "set xtics (\"\" 0)\n");
813     fprintf(gnustr, "set x2tics \n");
814     if (xtick != 0.0) fprintf(gnustr, "set x2tics %10.5g \n", xtick);
815   }
816   if (strcmp("BCMST", yopt) == 0) {
817     fprintf(gnustr, "set ytics (\"\" 0)\n");
818     /*  for some reason, range is not properly set for y2tics */
819     /*  fprintf(gnustr, "set y2tics \n");                     */
820     /*  fprintf(gnustr, "set y2range [*:*]\n");               */
821     if (ytick != 0.0)
822       /*  fprintf(gnustr, "set y2tics %10.5g \n", ytick);     */
823       fprintf(gnustr, "set ytics %10.5g \n", ytick);
824  }
825 
826   if (strcmp("ABCNST", xopt) == 0) {
827     fprintf(gnustr, "set xtics \n");
828     fprintf(gnustr, "set xzeroaxis \n");
829   }
830   if (strcmp("ABCNST", yopt) == 0) {
831     fprintf(gnustr, "set ytics \n");
832     fprintf(gnustr, "set yzeroaxis \n");
833   }
834 
835   fflush(gnustr);
836   return;
837 }
838 
839 /******************************************************************
840  @package   nightfall
841  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
842  @version   1.0
843  @short     Query plot window location
844  @tip       no support for units
845  @param     (see PGPLOT manual)
846  @return    (void)
847  @heading   PGPLOT emulation
848 *******************************************************************/
cpgqvp(int units,float * x1,float * x2,float * y1,float * y2)849 void cpgqvp(int units, float *x1, float *x2, float *y1, float *y2)
850 {
851   /* no support for units                                         */
852   if (units != 0) {
853     WARNING(_("PGPLOT emulation: cpgqvp has no support for units"));
854   } else {
855     *x1 = gnuMOrig_x; *x2 = gnuMOrig_x + gnuMSizex;
856     *y1 = gnuMOrig_y; *y2 = gnuMOrig_y + gnuMSizey;
857   }
858 }
859 
860 
861 /******************************************************************
862  @package   nightfall
863  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
864  @version   1.0
865  @short     Text annotation
866  @tip       hardcoded locations of annotations
867  @param     (see PGPLOT manual)
868  @return    (void)
869  @heading   PGPLOT emulation
870 *******************************************************************/
cpgmtxt(const char * side,float disp,float coord,float fjust,const char * text)871 void cpgmtxt(const char *side, float disp, float coord, float fjust,
872 const char *text)
873 {
874   float x = 0., y = 0.;      /* the text locations                */
875 
876    if (strcmp("T", side) == 0) {
877      y = 0.85; x = 0.5;
878    } else if (strcmp("B", side) == 0) {
879      y = 0.05; x = 0.8;
880    } else {
881      WARNING(_("PGPLOT emulation: cpgmtxt 'side' argument not valid"));
882    }
883 
884    fprintf(gnustr,
885 	   "set label \"%s\" at screen %5.2f, %5.2f center\n",
886 	   text, x, y);
887    fflush(gnustr);
888 }
889 
890 /******************************************************************
891  @package   nightfall
892  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
893  @version   1.0
894  @short     Set plot window with aspect ratio 1.0
895  @tip       works in GNUPLOT only with no title
896  @param     (see PGPLOT manual)
897  @return    (void)
898  @heading   PGPLOT emulation
899 *******************************************************************/
cpgwnad(float xleft,float xright,float ybot,float ytop)900 void cpgwnad(float xleft, float xright, float ybot, float ytop)
901 {
902    float xsize;                  /* size in x                     */
903    float ysize;                  /* size in y                     */
904    float max;                    /* max of both                   */
905 
906    /* set plot ranges                                             */
907 
908    fprintf(gnustr, "set xrange [%10.5g:%10.5g] \n", xleft, xright );
909    fprintf(gnustr, "set yrange [%10.5g:%10.5g] \n", ybot,  ytop );
910 
911    /* calculate sizes  and their maximum                          */
912    xsize = (xright - xleft)/gnuSizex;
913    ysize = (ytop   - ybot)/gnuSizey;
914    max   = MAX(xsize,ysize);
915 
916    /* adjust aspect                                               */
917    ysize = 1.375 * (ytop   - ybot)/max;
918    xsize = (xright - xleft)/max;
919 
920    /* adjust aspect of subpages                                   */
921    if (gnuSubpageflag == ON) {
922          ysize = ysize/gnuPageY;  xsize = xsize/gnuPageX;
923    }
924 
925    /* store for bookkeeping                                       */
926    gnuMSizey = ysize; gnuMSizex = xsize;
927 
928    /* set size                                                    */
929    fprintf(gnustr, "set size   %5.2f, %5.2f\n", xsize, ysize );
930    fflush(gnustr);    fflush(gnustr);
931 
932    return;
933 }
934 
935 /******************************************************************
936  @package   nightfall
937  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
938  @version   1.0
939  @short     Set plot window
940  @param     (see PGPLOT manual)
941  @return    (void)
942  @heading   PGPLOT emulation
943 *******************************************************************/
cpgswin(float xleft,float xright,float ybot,float ytop)944 void cpgswin(float xleft, float xright, float ybot, float ytop)
945 {
946    fprintf(gnustr, "set xrange [%10.5g:%10.5g] \n", xleft, xright );
947    fprintf(gnustr, "set yrange [%10.5g:%10.5g] \n", ybot,  ytop );
948    fprintf(gnustr, "unset autoscale\n");
949    fflush(gnustr);
950 
951    return;
952 }
953 
954 /******************************************************************
955  @package   nightfall
956  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
957  @version   1.0
958  @short     Set color
959  @tip       Color is linetype in GNUPLOT
960  @param     (see PGPLOT manual)
961  @return    (void)
962  @heading   PGPLOT emulation
963 *******************************************************************/
cpgsci(int lw)964 void cpgsci(int lw)
965 {
966   /* color = linetype in gnuplot                                  */
967   gnuLt = lw;
968   return;
969 }
970 
971 /******************************************************************
972  @package   nightfall
973  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
974  @version   1.0
975  @short     Set linewidth
976  @param     (see PGPLOT manual)
977  @return    (void)
978  @heading   PGPLOT emulation
979 *******************************************************************/
cpgslw(float lw)980 void cpgslw(float lw)
981 {
982    gnuLw = lw;
983    return;
984 }
985 
986 /******************************************************************
987  @package   nightfall
988  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
989  @version   1.0
990  @short     Set point size
991  @param     (see PGPLOT manual)
992  @return    (void)
993  @heading   PGPLOT emulation
994 *******************************************************************/
cpgsch(float lw)995 void cpgsch(float lw)
996 {
997    gnuPs = lw;
998    fprintf(gnustr, "set pointsize %5.2f \n", lw);
999    fflush(gnustr);
1000    return;
1001 }
1002 
1003 /******************************************************************
1004  @package   nightfall
1005  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
1006  @version   1.0
1007  @short     Set viewport
1008  @param     (see PGPLOT manual)
1009  @return    (void)
1010  @heading   PGPLOT emulation
1011 *******************************************************************/
cpgsvp(float xleft,float xright,float ybot,float ytop)1012 void cpgsvp(float xleft, float xright, float ybot, float ytop)
1013 {
1014    float xsize, ysize;        /* size of viewport                 */
1015    float xoff, yoff;          /* offset to origin                 */
1016 
1017    /* compute sizes and offset                                    */
1018    xoff = xleft;
1019    yoff = ybot;
1020    xsize = xright - xleft;
1021    ysize = ytop   - ybot;
1022 
1023    /* store for bookkeeping                                       */
1024    gnuSizex = xsize;
1025    gnuSizey = ysize;
1026 
1027    /* compute size and offset to origin for subpage               */
1028    if (gnuSubpageflag == ON) {
1029          xoff = gnuOrig_x + xoff/gnuPageX;
1030 	 yoff = gnuOrig_y + yoff/gnuPageY;
1031          ysize = ysize/gnuPageY;
1032 	 xsize = xsize/gnuPageX;
1033    }
1034 
1035    /* store for bookkeeping                                       */
1036    gnuMSizey = ysize;
1037    gnuMSizex = xsize;
1038    gnuMOrig_y = yoff;
1039    gnuMOrig_x = xoff;
1040 
1041    /* set viewport and initialize                                 */
1042 
1043    fprintf(gnustr, "set title ""\n");
1044    fprintf(gnustr, "set xlabel\n");
1045    fprintf(gnustr, "set ylabel\n");
1046    fprintf(gnustr, "set xrange [*:*]\n");
1047    fprintf(gnustr, "set yrange [*:*]\n");
1048    fprintf(gnustr, "set autoscale\n");
1049    fprintf(gnustr, "unset xzeroaxis \n");
1050    fprintf(gnustr, "unset yzeroaxis \n");
1051    fprintf(gnustr, "unset y2tics \n");
1052    fprintf(gnustr, "unset x2tics \n");
1053    fprintf(gnustr, "unset label\n");
1054    fprintf(gnustr, "set mxtics 5\n");
1055    fprintf(gnustr, "set mytics 5\n");
1056    fprintf(gnustr, "set origin %5.2f, %5.2f\n", xoff,  yoff );
1057    fprintf(gnustr, "set size   %5.2f, %5.2f\n", xsize, ysize );
1058    fflush(gnustr);
1059 
1060    return;
1061 }
1062 
1063 /******************************************************************
1064  @package   nightfall
1065  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
1066  @version   1.0
1067  @short     Set viewport
1068  @param     (see PGPLOT manual)
1069  @return    (void)
1070  @heading   PGPLOT emulation
1071 *******************************************************************/
cpgenv(float xmin,float xmax,float ymin,float ymax,int just,int axis)1072 void cpgenv(float xmin, float xmax, float ymin, float ymax, int just, int axis)
1073 {
1074   cpgsvp(xmin, xmax, ymin, ymax);
1075   return;
1076 }
1077 
1078 /******************************************************************
1079  @package   nightfall
1080  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
1081  @version   1.0
1082  @short     Set subpage
1083  @param     (see PGPLOT manual)
1084  @return    (void)
1085  @heading   PGPLOT emulation
1086 *******************************************************************/
cpgsubp(int a,int b)1087 void cpgsubp(int a, int b)
1088 {
1089   /* store number of subpages                                     */
1090   gnuPageX = a;
1091   gnuPageY = b;
1092 
1093   /* initialize current subpage                                   */
1094   gnuPageXn = 0;
1095   gnuPageYn = -1;
1096 
1097   fprintf(gnustr, "unset multiplot\n");
1098   fprintf(gnustr, "set multiplot\n");
1099   fprintf(gnustr, "set origin 0.0, 0.0\n");
1100   fflush(gnustr);
1101 
1102   /* initialize origin of current subpage                         */
1103   gnuMOrig_y = 0.; gnuMOrig_x = 0.;
1104 
1105   gnuSubpageflag = ON;
1106   return;
1107 }
1108 
1109 /******************************************************************
1110  @package   nightfall
1111  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
1112  @version   1.0
1113  @short     Goto next page
1114  @param     (see PGPLOT manual)
1115  @return    (void)
1116  @heading   PGPLOT emulation
1117 *******************************************************************/
cpgpage()1118 void cpgpage()
1119 {
1120   float orig_x, orig_y;       /* origin of next page              */
1121 
1122   orig_y = 0.0;
1123   orig_x = 0.0;
1124 
1125   if (gnuSubpageflag == ON)  {
1126 
1127       /* ------------  multiplot ------------------------------   */
1128 
1129       /* page n times offset                                      */
1130       if (gnuPageYn < gnuPageY) ++gnuPageYn;
1131       if (gnuPageYn == gnuPageY) {
1132         gnuPageYn = 0;
1133         ++gnuPageXn;
1134         if (gnuPageXn == gnuPageX) {
1135               gnuSubpageflag = OFF;
1136               gnuPageXn = 0; gnuPageYn = 0;
1137 
1138               fprintf(gnustr, "unset multiplot\n");
1139               fprintf(gnustr, "set multiplot\n");
1140               fflush(gnustr);
1141 
1142 	}
1143       }
1144       gnuOrig_x = orig_x + gnuPageXn * (1.0 / gnuPageX);
1145       gnuOrig_y = orig_y + gnuPageYn * (1.0 / gnuPageY);
1146       gnuMOrig_y = gnuOrig_y; gnuMOrig_x = gnuOrig_x;
1147       fprintf(gnustr, "set origin %5.2f ,%5.2f\n", gnuOrig_x, gnuOrig_y);
1148       fflush(gnustr);
1149 
1150   } else {
1151 
1152       /* ------------  single page ----------------------------   */
1153 
1154       gnuMOrig_y = 0.; gnuMOrig_x = 0.;
1155 
1156       fprintf(gnustr, "unset multiplot\n");
1157       fprintf(gnustr, "set multiplot\n");
1158       fprintf(gnustr, "set origin 0.0 ,0.0\n");
1159       fflush(gnustr);
1160 
1161   }
1162 }
1163 
1164 /******************************************************************
1165  @package   nightfall
1166  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
1167  @version   1.0
1168  @short     Open plot device
1169  @param     (see PGPLOT manual)
1170  @return    (int)    The error code
1171  @heading   PGPLOT emulation
1172 *******************************************************************/
cpgopen(char * s)1173 int cpgopen(char *s)
1174 {
1175   char gnu_geometry[256];        /*  Window geometry              */
1176   int result;                    /*  Error code                   */
1177   char tmpdir[1024];             /*  tmp directory                */
1178   struct sigaction act, oldact;  /*  signal handling              */
1179 
1180   /* xwindow already open                                         */
1181 
1182   if (strcmp("/XSERVE", s) == 0 && Flags.plotOpened == ON) {
1183      fprintf(gnustr, "set terminal x11\n");
1184      fflush(gnustr);
1185      /* seems to be reqired unfortunately                         */
1186      nanosleep (&sleep_req, &sleep_rem);
1187      fprintf(gnustr, "set terminal x11\n");
1188      fprintf(gnustr, "unset mouse\n"); /* requires 3.8c+          */
1189      fflush(gnustr);
1190      return (1);
1191   }
1192   else if (Flags.plotOpened == ON) {
1193      fprintf(gnustr, "set terminal postscript color\n");
1194      fprintf(gnustr, "set output \"%s\" \n", Out_Plot_File);
1195      fflush(gnustr);
1196      return (1);
1197   }
1198 
1199   /* get tmp directory                                            */
1200 
1201   if (getenv("TEMPDIR") != NULL) strncpy(tmpdir, getenv("TEMPDIR"), 960);
1202   if (getenv("TMPDIR")  != NULL) strncpy(tmpdir, getenv("TMPDIR"), 960);
1203   else strcpy(tmpdir, "/tmp");
1204 
1205   strcpy(gnuCommands, tmpdir);
1206   strcpy(gnuData1, tmpdir);
1207   strcpy(gnuData2, tmpdir);
1208   strcpy(gnuData3, tmpdir);
1209   strcpy(gnuData4, tmpdir);
1210 
1211   /* create command pipe                                          */
1212 
1213   sprintf(gnuCommands + strlen(gnuCommands), "/gnuplot.input.%ld",
1214 	  (long) getpid() );
1215   result = mkfifo(gnuCommands, 0666);
1216   if(result) perror (_("could not create command pipe"));
1217 
1218   /* create first data pipe                                       */
1219 
1220   sprintf(gnuData1 + strlen(gnuData1), "/gnuplot.data1.%ld",
1221 	  (long) getpid() );
1222   result = mkfifo(gnuData1, 0666);
1223   if(result) perror (_("could not create data1 pipe"));
1224 
1225 
1226   /* create second data pipe                                      */
1227 
1228   sprintf(gnuData2 + strlen(gnuData2), "/gnuplot.data2.%ld",
1229 	  (long) getpid() );
1230   result = mkfifo(gnuData2, 0666);
1231   if(result) perror (_("could not create data2 pipe"));
1232 
1233 
1234   /* create third data pipe                                       */
1235 
1236   sprintf(gnuData3 + strlen(gnuData3), "/gnuplot.data3.%ld",
1237 	  (long) getpid() );
1238   result = mkfifo(gnuData3, 0666);
1239   if(result) perror (_("could not create data3 pipe"));
1240 
1241   /* create forth data pipe                                       */
1242 
1243   sprintf(gnuData4 + strlen(gnuData4), "/gnuplot.data4.%ld",
1244 	  (long) getpid() );
1245   result = mkfifo(gnuData4, 0666);
1246   if(result) perror (_("could not create data4 pipe"));
1247 
1248 
1249   /* fork a process for GNUPLOT                                   */
1250 
1251   childPid = fork();
1252 
1253   if ( childPid == -1 ) {
1254       perror (_("unable to fork"));
1255       _exit (EXIT_FAILURE);
1256       return 0;
1257     }
1258 
1259   if ( childPid == 0 ){
1260 
1261     /* ------------ child process -----------------------------   */
1262 
1263     processGroup = getpid();
1264 
1265     setpgid(0, processGroup);
1266 
1267     if (getenv("GNUPLOT_GEOMETRY") == NULL)
1268       strncpy(gnu_geometry, GNU_GEOMETRY, 255);
1269     else
1270       strncpy(gnu_geometry, getenv("GNUPLOT_GEOMETRY"), 255);
1271     gnu_geometry[255] = '\0';
1272 
1273     fclose(stdin);
1274     stdin = fopen("/dev/null", "r");
1275 
1276     /* start GNUPLOT and exit                                     */
1277 
1278     if (strcmp("/XSERVE", s) == 0) {
1279       /*
1280       fprintf(stderr, "gnuplot -persist -geometry %s %s\n",
1281 	      gnu_geometry, gnuCommands);
1282       */
1283       execlp("gnuplot", "gnuplot", "-persist", "-geometry",
1284 	     gnu_geometry, gnuCommands, NULL);
1285     } else {
1286       /*
1287       fprintf(stderr, "gnuplot -geometry %s %s\n",
1288 	      gnu_geometry, gnuCommands);
1289       */
1290       execlp("gnuplot", "gnuplot", "-geometry",
1291 	     gnu_geometry, gnuCommands, NULL);
1292     }
1293 
1294     /* execlp only returns if an error has occured               */
1295     perror (_("could not execute gnuplot"));
1296 
1297     _exit(EXIT_FAILURE);
1298     return 0;
1299 
1300   } else {
1301 
1302     /* ------------- parent process ---------------------------  */
1303 
1304     if (Flags.interactive == ON)
1305       setpgid ( childPid, processGroup);
1306 
1307     /* open command pipe for write and return                    */
1308 
1309     gnustr = fopen(gnuCommands, "w");
1310     if(!gnustr) perror (_("could not open command pipe"));
1311 
1312     /* set up a signal handler so we can clean up if killed      */
1313 
1314     act.sa_handler = &gnu_sighandler; /* signal action           */
1315     sigemptyset( &act.sa_mask );      /* set an empty mask       */
1316     act.sa_flags = 0;                 /* init sa_flags           */
1317 
1318 #ifdef SIGHUP
1319     sigaction(SIGHUP,  &act, &oldact);
1320 #endif
1321 #ifdef SIGINT
1322     sigaction(SIGINT,  &act, &oldact);
1323 #endif
1324 #ifdef SIGQUIT
1325     sigaction(SIGQUIT, &act, &oldact);
1326 #endif
1327 #ifdef SIGILL
1328     sigaction(SIGILL,  &act, &oldact);
1329 #endif
1330 #ifdef SIGABRT
1331     sigaction(SIGABRT, &act, &oldact);
1332 #endif
1333 #ifdef SIGSEGV
1334     sigaction(SIGSEGV, &act, &oldact);
1335 #endif
1336 #ifdef SIGPIPE
1337     sigaction(SIGPIPE, &act, &oldact);
1338 #endif
1339 #ifdef SIGTRAP
1340     sigaction(SIGTRAP, &act, &oldact);
1341 #endif
1342 #ifdef SIGIOT
1343     sigaction(SIGIOT,  &act, &oldact);
1344 #endif
1345 #ifdef SIGTERM
1346     sigaction(SIGTERM, &act, &oldact);
1347 #endif
1348 #ifdef SIGBUS
1349     sigaction(SIGBUS,  &act, &oldact);
1350 #endif
1351 #ifdef SIGIO
1352     sigaction(SIGIO,   &act, &oldact);
1353 #endif
1354 #ifdef SIGPOLL
1355     sigaction(SIGPOLL, &act, &oldact);
1356 #endif
1357 #ifdef SIGXCPU
1358     sigaction(SIGXCPU, &act, &oldact);
1359 #endif
1360 #ifdef SIGPWR
1361     sigaction(SIGPWR,  &act, &oldact);
1362 #endif
1363 
1364 
1365     if (strcmp("/XSERVE", s) == 0 || strcmp("/XNOP", s) == 0  ) {
1366      fprintf(gnustr, "set terminal x11\n");
1367      fprintf(gnustr, "unset mouse\n"); /* requires 3.8c+          */
1368      fflush(gnustr);
1369     } else {
1370      fprintf(gnustr, "set terminal postscript color\n");
1371      fprintf(gnustr, "set output \"%s\" \n", Out_Plot_File);
1372      fflush(gnustr);
1373     }
1374 
1375     fprintf(gnustr, "set origin 0.0, 0.0\n");
1376     fflush(gnustr);
1377     gnuSubpageflag = OFF;
1378 
1379     if (strcmp("/XSERVE", s) == 0) Flags.plotOpened = ON;
1380 
1381     return(1);
1382   }
1383 }
1384 
1385 /******************************************************************
1386  @package   nightfall
1387  @author    Rainer Wichmann (rwichman@lsw.uni-heidelberg.de)
1388  @version   1.0
1389  @short     Close plot device
1390  @param     (see PGPLOT manual)
1391  @return    (void)
1392  @heading   PGPLOT emulation
1393 *******************************************************************/
cpgend()1394 void cpgend()
1395 {
1396   /*
1397   static char command[80], line[133], *linep, *token;
1398   FILE *fp;
1399   int try;
1400   */
1401 
1402     fprintf(gnustr, "unset multiplot\n");
1403     fflush(gnustr);
1404     fprintf(gnustr, "unset multiplot\n");
1405     fflush(gnustr);
1406     fprintf(gnustr, "set output \n ");
1407     fflush(gnustr);
1408 
1409     /* -------  return if plot window is open ----------------    */
1410 
1411     if (Flags.plotOpened == ON) {
1412       fprintf(gnustr, "set terminal x11\n ");
1413       fflush(gnustr);
1414       return;
1415     }
1416 
1417     /* ------- else                           ----------------    */
1418 
1419     /* required for proper closedown of pipes                     */
1420     nanosleep (&sleep_req, &sleep_rem);
1421 
1422     remove(gnuData1);
1423     remove(gnuData2);
1424     remove(gnuData3);
1425     remove(gnuData4);
1426     fclose(gnustr);
1427 
1428     if (Flags.interactive == ON)
1429       kill ( -childPid, SIGKILL);
1430     else
1431       kill (  childPid, SIGKILL);
1432 
1433     remove(gnuCommands);
1434 
1435     return;
1436 }
1437 
1438 #endif
1439 
1440