1 #ifndef _GNUPLOT_PIPES_C_
2 #define _GNUPLOT_PIPES_C_
58 #define GP_CMD_SIZE 2048
60 #define GP_TITLE_SIZE 80
62 #define GP_EQ_SIZE 512
64 #define PATH_MAXNAMESZ 4096
74 #define GNUPLOT_TEMPFILE "%s/gnuplot-i-XXXXXX"
76 #define GNUPLOT_EXEC "gnuplot"
78 #define GNUPLOT_EXEC "pgnuplot.exe"
89 int mkstemp(
char* templ) {
92 char *start=strstr(templ,
"XXXXXX");
94 start[i]=(char) (48+((
int) rand()*10/32768.0));
98 i=open(templ,O_RDWR | O_CREAT);
100 DWORD dwFileAttr=GetFileAttributes(templ);
101 SetFileAttributes(templ,dwFileAttr & !FILE_ATTRIBUTE_READONLY);
144 sprintf(buf,
"./%s", pname) ;
145 if (access(buf, X_OK)==0) {
151 path = getenv(
"PATH") ;
153 for (i=0; path[i]; ) {
154 for (j=i ; (path[j]) && (path[j]!=
':') ; j++);
156 strncpy(buf, path + i, lg);
157 if (lg == 0) buf[lg++] =
'.';
159 strcpy(buf + lg, pname);
160 if (access(buf, X_OK) == 0) {
166 if (path[i] ==
':') i++ ;
169 fprintf(stderr,
"PATH variable not set\n");
172 if (buf[0] == 0)
return NULL ;
174 lg = strlen(buf) - 1 ;
175 while (buf[lg]!=
'/') {
201 if (getenv(
"DISPLAY") == NULL) {
202 fprintf(stderr,
"cannot find DISPLAY variable: is it set?\n") ;
208 fprintf(stderr,
"cannot find gnuplot in your PATH");
213 fprintf(stderr,
"cannot find gnuplot in your PATH");
226 if (handle->
gnucmd == NULL) {
227 fprintf(stderr,
"error starting gnuplot\n") ;
238 if (getenv(
"DISPLAY") == NULL || (getenv(
"USE_AQUA")!=NULL && strcmp(getenv(
"USE_AQUA"),
"1")>=0))
265 if (pclose(handle->
gnucmd) == -1) {
266 fprintf(stderr,
"problem closing communication to gnuplot\n") ;
270 for (i=0 ; i<handle->
ntmp ; i++) {
274 printf(
"Failure to delate %s: error number %d\n",handle->
to_delete[i],errno);
319 vsprintf(local_cmd, cmd, ap);
322 strcat(local_cmd,
"\n");
324 fputs(local_cmd, handle->
gnucmd) ;
346 printf(
"Entering interactive gnuplot mode (experimental) ...\n");
355 printf(
"Leaving interactive gnuplot mode ... \n");
387 if (strcmp(plot_style,
"lines") &&
388 strcmp(plot_style,
"points") &&
389 strcmp(plot_style,
"linespoints") &&
390 strcmp(plot_style,
"impulses") &&
391 strcmp(plot_style,
"dots") &&
392 strcmp(plot_style,
"steps") &&
393 strcmp(plot_style,
"errorbars") &&
394 strcmp(plot_style,
"boxes") &&
395 strcmp(plot_style,
"boxerrorbars") &&
396 strcmp(plot_style,
"points palette")) {
397 fprintf(stderr,
"warning: unknown requested style: using points\n") ;
398 strcpy(h->
pstyle,
"points") ;
400 strcpy(h->
pstyle, plot_style) ;
424 if (strcmp(plot_axes,
"x1y1") &&
425 strcmp(plot_axes,
"x1y2") &&
426 strcmp(plot_axes,
"x2y1") &&
427 strcmp(plot_axes,
"x2y2")) {
428 fprintf(stderr,
"warning: unknown requested axes: using default\n") ;
429 sprintf(h->
axes,
" ") ;
431 sprintf(h->
axes,
"axes %s", plot_axes) ;
452 strncpy(h->
term, terminal,32);
454 sprintf(cmd,
"set terminal %s",h->
term);
474 sprintf(cmd,
"set xlabel \"%s\"", label) ;
495 sprintf(cmd,
"set ylabel \"%s\"", label) ;
515 sprintf(cmd,
"set zlabel \"%s\"", label) ;
535 for (i=0 ; i<h->
ntmp ; i++) {
592 if (handle==NULL || d==NULL || (n<1)) return ;
597 "maximum # of temporary files reached (%d): cannot open more",
604 if ((tmpfd=mkstemp(name))==-1) {
605 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
613 for (i=0 ; i<n ; i++) {
614 sprintf(line,
"%g\n", d[i]);
615 ret = write(tmpfd, line, strlen(line));
616 if(ret != (
int)strlen(line)) {
618 fprintf(stderr,
"error writing to temp file\n");
621 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
631 strcpy(cmd,
"replot") ;
633 strcpy(cmd,
"plot") ;
637 sprintf(line,
"%s \"%s\" %s with %s", cmd, name, handle->
axes,
640 sprintf(line,
"%s \"%s\" %s title \"%s\" with %s", cmd, name,
699 if (handle==NULL || x==NULL || y==NULL || (n<1)) return ;
704 "maximum # of temporary files reached (%d): cannot open more",
711 if ((tmpfd=mkstemp(name))==-1) {
712 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
720 for (i=0 ; i<n; i++) {
721 sprintf(line,
"%g %g\n", x[i], y[i]) ;
722 ret = write(tmpfd, line, strlen(line));
723 if(ret != (
int)strlen(line)) {
725 fprintf(stderr,
"error writing to temp file:\n");
728 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
738 strcpy(cmd,
"replot") ;
740 strcpy(cmd,
"plot") ;
744 sprintf(line,
"%s \"%s\" %s with %s", cmd, name,
747 sprintf(line,
"%s \"%s\" %s title \"%s\" with %s", cmd, name,
800 if (handle==NULL || x==NULL || y==NULL || (n<1))
return 1;
806 "maximum # of temporary files reached (%d): cannot open more",
813 if ((tmpfd=mkstemp(name))==-1) {
814 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
822 for (i=0 ; i<n; i++) {
823 sprintf(line,
"%g %g %g\n", x[i], y[i], z[i]) ;
824 ret = write(tmpfd, line, strlen(line));
825 if(ret != (
int)strlen(line)) {
827 fprintf(stderr,
"error writing to temp file\n");
830 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
839 strcpy(cmd,
"splot") ;
842 sprintf(line,
"%s \"%s\" with %s", cmd, name, handle->
pstyle) ;
844 sprintf(line,
"%s \"%s\" title \"%s\" with %s", cmd, name,
880 if (handle==NULL || points==NULL || (rows<1) || (cols<1))
return 1;
886 "maximum # of temporary files reached (%d): cannot open more",
893 if ((tmpfd=mkstemp(name))==-1) {
894 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
902 for (i=0 ; i<rows; i++) {
903 for (j=0;j<cols;j++) {
904 sprintf(line,
"%d %d %g\n", i,j,points[i*cols+j]) ;
905 ret = write(tmpfd, line, strlen(line));
906 if(ret != (
int)strlen(line)) {
908 fprintf(stderr,
"error writing to file temp file\n");
911 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
918 ret = write(tmpfd, line, strlen(line));
919 if(ret != (
int)strlen(line)) {
921 fprintf(stderr,
"error writing to temp file\n");
924 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
933 strcpy(cmd,
"splot") ;
936 sprintf(line,
"%s \"%s\" with %s", cmd, name, handle->
pstyle) ;
938 sprintf(line,
"%s \"%s\" title \"%s\" with %s", cmd, name,
997 if (handle==NULL || x==NULL || y==NULL || (nx<1) || (ny<1))
return 1;
1003 "maximum # of temporary files reached (%d): cannot open more",
1010 if ((tmpfd=mkstemp(name))==-1) {
1011 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
1020 for (i=0 ; i<nx; i++) {
1021 for (j=0 ; j<ny; j++) {
1022 sprintf(line,
"%g %g %g\n", x[nx*i+j], y[nx*i+j], z[nx*i+j]) ;
1023 ret = write(tmpfd, line, strlen(line));
1024 if(ret != (
int)strlen(line)) {
1026 fprintf(stderr,
"error writing to temp file\n");
1029 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
1035 sprintf(line,
"\n") ;
1036 ret = write(tmpfd, line, strlen(line));
1037 if(ret != (
int)strlen(line)) {
1039 fprintf(stderr,
"error writing to temp file\n");
1042 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
1055 strcpy(cmd,
"splot") ;
1057 if (title == NULL) {
1058 sprintf(line,
"%s \"%s\" with %s", cmd, name, handle->
pstyle) ;
1060 sprintf(line,
"%s \"%s\" title \"%s\" with %s", cmd, name,
1103 if (handle==NULL || getPoint==NULL || (n<1))
return 1;
1109 "maximum # of temporary files reached (%d): cannot open more",
1116 if ((tmpfd=mkstemp(name))==-1) {
1117 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
1127 getPoint(obj,&point,i,n);
1128 sprintf(line,
"%g %g %g\n", point.
x, point.
y, point.
z) ;
1129 ret = write(tmpfd, line, strlen(line));
1130 if(ret != (
int)strlen(line)) {
1132 fprintf(stderr,
"error writing to temp file\n");
1135 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
1144 strcpy(cmd,
"splot") ;
1146 if (title == NULL) {
1147 sprintf(line,
"%s \"%s\" with %s", cmd, name, handle->
pstyle) ;
1149 sprintf(line,
"%s \"%s\" title \"%s\" with %s", cmd, name,
1216 if (handle==NULL || getPoint==NULL || (n<1))
return 1;
1221 "maximum # of temporary files reached (%d): cannot open more",
1228 if ((tmpfd=mkstemp(name))==-1) {
1229 fprintf(stderr,
"cannot create temporary file: exiting plot") ;
1238 for (i=0 ; i<n; i++) {
1239 getPoint(obj,&point,i,n);
1240 sprintf(line,
"%g %g\n", point.
x, point.
y) ;
1241 ret = write(tmpfd, line, strlen(line));
1242 if(ret != (
int)strlen(line)) {
1244 fprintf(stderr,
"error writing to temp file\n");
1247 fprintf(stderr,
"could not complete write %d of %d bytes written\n", ret, (
int)strlen(line));
1256 if (handle->
nplots > 0) {
1257 strcpy(cmd,
"replot") ;
1259 strcpy(cmd,
"plot") ;
1262 if (title == NULL) {
1263 sprintf(line,
"%s \"%s\" %s with %s", cmd, name,
1266 sprintf(line,
"%s \"%s\" %s title \"%s\" with %s", cmd, name,
1309 if (x==NULL || n<1) return ;
1317 if (label_x!=NULL) {
1322 if (label_y!=NULL) {
1332 printf(
"press ENTER to continue\n");
1333 while (getchar()!=
'\n') {}
1378 if (title == NULL) {
1379 strcpy(stitle,
"no title") ;
1381 strcpy(stitle, title) ;
1384 if (handle->
nplots > 0) {
1385 sprintf(cmd,
"replot %g * x + %g title \"%s\" with %s",
1386 a, b, title, handle->
pstyle) ;
1388 sprintf(cmd,
"plot %g * x + %g title \"%s\" with %s",
1389 a, b, title, handle->
pstyle) ;
1432 if (title == NULL) {
1433 strcpy(title_str,
"no title") ;
1435 strcpy(title_str, title) ;
1438 strcpy(plot_str,
"replot") ;
1440 strcpy(plot_str,
"plot") ;
1443 sprintf(cmd,
"%s %s title \"%s\" with %s",
1444 plot_str, equation, title_str, h->
pstyle) ;
1508 gnuplot_cmd(h,
"set terminal postscript enhanced color");