FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Emulation of popen/pclose for wgnuplot via temporary files · gnuplot/gnuplot@ab23b20 · GitHub

Repository navigation

Commit ab23b20

Browse files
committed
Emulation of popen/pclose for wgnuplot via temporary files
1 parent cc26490 commit ab23b20

8 files changed

Lines changed: 158 additions & 28 deletions

File tree

‎ChangeLog‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2011-12-06 Bastian Maerkisch <bmaerkisch@web.de>
2+
3+
* src/win/winmain.c src/win/wtext.h config/config.mgw config/config.nt
4+
config/mingw/Makefile config/msvc/Makefile: Emulation of popen/pclose
5+
for wgnuplot via temporary files. Briefly opens a console window for
6+
every pipe and read and write pipes are supported.
7+
18
2011-12-05 Bastian Maerkisch <bmaerkisch@web.de>
29

310
* src/win/winmain.c: Automatic language detection can be overridden

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ New features, changes and fixes since gnuplot version 4.6
33

44
* NEW geographic (DMS) data axes
55
* NEW set arrow <tag> from <start> length <len> angle <ang>
6+
* CHANGE Pipe (popen/pclose) emulation within wgnuplot
67
* CHANGE Draw the z-axis label at a fixed distance to the left of the z-axis
78
* FIX various improvements to the qt terminal
89

‎config/config.mgw‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,5 +606,14 @@
606606

607607
#define GP_INLINE inline
608608

609+
/* Emulated pipe support for wgnuplot */
610+
#ifdef USE_FAKEPIPES
611+
# ifndef PIPES
612+
# define PIPES
613+
# endif
614+
# define popen fake_popen
615+
# define pclose fake_pclose
616+
#endif
617+
609618
/* gnuplot.gih is not used by wgnuplot */
610619
#define NO_GIH 1

‎config/config.nt‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,18 @@
591591
#define GP_MATHERR _matherr
592592
#define snprintf _snprintf
593593
/*#define vsnprintf _vsnprintf*/
594-
#ifdef PIPES
594+
#define isnan _isnan
595+
#define ftruncate _chsize
596+
597+
/* Emulated pipe support for wgnuplot */
598+
#ifdef USE_FAKEPIPES
599+
# define PIPES
600+
# define popen fake_popen
601+
# define pclose fake_pclose
602+
#elif defined(PIPES)
595603
# define popen _popen
596604
# define pclose _pclose
597605
#endif
598-
#define isnan _isnan
599-
#define ftruncate _chsize
600606

601607
/* gnuplot.gih is not used by wgnuplot */
602608
#define NO_GIH 1

‎config/mingw/Makefile‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Hello, emacs: this is a -*- Makefile -*-
2-
# $Id: Makefile,v 1.16 2011/11/26 14:24:26 markisch Exp $
2+
# $Id: Makefile,v 1.17 2011/11/27 22:17:38 markisch Exp $
33
#
44
# GNUPLOT Makefile for MinGW/MSYS on Windows
55
# This assumes usual installation of the MinGW32 toolkit, the free
@@ -200,9 +200,9 @@ ifeq ($(TARGET),wgnuplot.exe)
200200
PGNUPLOT=pgnuplot.exe
201201
MENUFILE=wgnuplot.mnu
202202
O=o
203+
CFLAGSEXTRA=-DUSE_FAKEPIPES
203204
endif
204205
ifeq ($(TARGET),wgnuplot_pipes.exe)
205-
PGNUPLOT=pgnuplot.exe
206206
MENUFILE=wgnuplot.mnu
207207
PIPES=1
208208
O=po
@@ -438,7 +438,7 @@ endif
438438
# default rules
439439
.SUFFIXES: .exe .$(O) .c
440440
.c.$(O):
441-
$(CC) -c $(CFLAGS) -o $@ $<
441+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
442442

443443
LDLIBS = -lkernel32 -lgdi32 -lwinspool -lcomdlg32 -lcomctl32 -ladvapi32 -lshell32 -lmsimg32 -lgdiplus
444444

@@ -461,10 +461,10 @@ wgplt_res.$(O) : $(W)/wgnuplot.rc $(W)/wgnuplib.rc $(W)/wresourc.h texticon.ico
461461
$(RES2COFF)
462462

463463
show.$(O): show.c plot.h setshow.h
464-
$(CC) -c $(CFLAGS) -DHELPFILE=\"$(HELPFILE)\" -DBINDIR=\"$(bindir)\" -DGNUPLOT_PS_DIR=\"$(GNUPLOT_PS_DIR)\" -o $@ $<
464+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -DHELPFILE=\"$(HELPFILE)\" -DBINDIR=\"$(bindir)\" -DGNUPLOT_PS_DIR=\"$(GNUPLOT_PS_DIR)\" -o $@ $<
465465

466466
term.$(O): term.c term.h plot.h setshow.h bitmap.h $(CORETERM)
467-
$(CC) -c $(CFLAGS) $(TERMFLAGS) -o $@ $<
467+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) $(TERMFLAGS) -o $@ $<
468468

469469
version.$(O): $(S)/version.c
470470

@@ -476,43 +476,43 @@ endif
476476
WINDEPS = $(W)/wgnuplib.h $(W)/wcommon.h $(W)/wresourc.h $(W)/screenbuf.h $(W)/wgdiplus.h
477477

478478
winmain.$(O): $(W)/winmain.c $(W)/wgnuplib.h $(W)/wtext.h plot.h
479-
$(CC) -c $(CFLAGS) -o $@ -DHELPFILE=\"$(HELPFILE)\" $<
479+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ -DHELPFILE=\"$(HELPFILE)\" $<
480480

481481
wgnuplib.$(O): $(W)/wgnuplib.c $(WINDEPS)
482-
$(CC) -c $(CFLAGS) -o $@ $<
482+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
483483

484484
wmenu.$(O): $(W)/wmenu.c $(WINDEPS)
485-
$(CC) -c $(CFLAGS) -o $@ $<
485+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
486486

487487
wtext.$(O): $(W)/wtext.c $(WINDEPS)
488-
$(CC) -c $(CFLAGS) -o $@ $<
488+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
489489

490490
screenbuf.$(O): $(W)/screenbuf.c $(WINDEPS)
491-
$(CC) -c $(CFLAGS) -o $@ $<
491+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
492492

493493
wpause.$(O): $(W)/wpause.c $(S)/wxterminal/wxt_term.h $(WINDEPS)
494-
$(CC) -c $(CFLAGS) -o $@ $<
494+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
495495

496496
wprinter.$(O): $(W)/wprinter.c $(WINDEPS)
497-
$(CC) -c $(CFLAGS) -o $@ $<
497+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
498498

499499
wgraph.$(O): $(W)/wgraph.c $(WINDEPS)
500-
$(CC) -c $(CFLAGS) -o $@ $<
500+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
501501

502502
wgdiplus.$(O): $(W)/wgdiplus.cpp $(WINDEPS)
503-
$(CXX) -c $(CXXFLAGS) -o $@ $<
503+
$(CXX) -c $(CXXFLAGS) $(CFLAGSEXTRA) -o $@ $<
504504

505505
wgnuplot.mnu: $(W)/wgnuplot.mnu
506506
$(CP) $^ $@
507507

508508
wxt_gui.$(O): $(S)/wxterminal/wxt_gui.cpp $(S)/wxterminal/wxt_gui.h
509-
$(CXX) -c $(CFLAGS) $(CXXFLAGS) $(CAIRO_CFLAGS) -o $@ $<
509+
$(CXX) -c $(CFLAGS) $(CFLAGSEXTRA) $(CXXFLAGS) $(CAIRO_CFLAGS) -o $@ $<
510510

511511
gp_cairo.$(O): $(S)/wxterminal/gp_cairo.c $(S)/wxterminal/gp_cairo.h
512-
$(CC) -c $(CFLAGS) $(CXXFLAGS) $(PANGOCAIRO_CFLAGS) -o $@ $<
512+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) $(CXXFLAGS) $(PANGOCAIRO_CFLAGS) -o $@ $<
513513

514514
gp_cairo_helpers.$(O): $(S)/wxterminal/gp_cairo_helpers.c $(S)/wxterminal/gp_cairo_helpers.h
515-
$(CC) -c $(CFLAGS) -o $@ $<
515+
$(CC) -c $(CFLAGS) $(CFLAGSEXTRA) -o $@ $<
516516

517517
# extract icons from wgnuplot.rc
518518
texticon.ico: grpicon.ico

‎config/msvc/Makefile‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# $Id: Makefile,v 1.2 2011/10/30 18:31:01 markisch Exp $
2+
# $Id: Makefile,v 1.3 2011/11/27 22:18:36 markisch Exp $
33
#
44
# GNUPLOT Makefile Microsoft Visual C++ 2005 or newer
55

@@ -85,6 +85,7 @@ LD = link
8585
# /MD means include multi-thread support (DLL)
8686
CBASEFLAGS = /EHsc /GR /MD /O2 /nologo
8787
CFLAGS = $(CBASEFLAGS) /I. /I$(TOP) /I$(S) /D_Windows /DWIN32 /DHAVE_CONFIG_H /D__MSC__ /DHELPFILE=\"$(HELPFILE)\" $(OPTIONS)
88+
WIN_CFLAGS = /DUSE_FAKEPIPES
8889
CONSOLE_CFLAGS = /DWGP_CONSOLE /DCONSOLE_SWITCH_CP /DPIPES
8990
OPTIONS = /DUSE_MOUSE
9091
OPTIONS = $(OPTIONS) /DGNUPLOT_HISTORY
@@ -165,19 +166,19 @@ ALL_CONSOLE_OBJS = $(OBJS:.obj=.cobj) $(WINOBJS:.obj=.cobj)
165166

166167
# default rules
167168
{$(S)}.c.obj:
168-
$(CC) /c $(CFLAGS) $(S)\\$*.c
169+
$(CC) /c $(CFLAGS) $(WIN_CFLAGS) $(S)\\$*.c
169170

170171
{$(S)}.c.cobj:
171172
$(CC) /c $(CFLAGS) $(CONSOLE_CFLAGS) $(S)\\$*.c /Fo$@
172173

173174
{$(W)}.c.obj:
174-
$(CC) /c $(CFLAGS) $(W)$*.c /Fo$@
175+
$(CC) /c $(CFLAGS) $(WIN_CFLAGS) $(W)$*.c /Fo$@
175176

176177
{$(W)}.c.cobj:
177178
$(CC) /c $(CFLAGS) $(CONSOLE_CFLAGS) $(W)$*.c /Fo$@
178179

179180
{$(W)}.cpp.obj:
180-
$(CC) /c $(CFLAGS) $(W)$*.cpp /Fo$@
181+
$(CC) /c $(CFLAGS) $(WIN_CFLAGS) $(W)$*.cpp /Fo$@
181182

182183
{$(W)}.cpp.cobj:
183184
$(CC) /c $(CFLAGS) $(CONSOLE_CFLAGS) $(W)$*.cpp /Fo$@
@@ -257,13 +258,13 @@ linkopt-c.msw: Makefile
257258
# rules
258259

259260
term.obj: $(S)\term.c $(S)\term.h $(S)\plot.h $(S)\setshow.h $(S)\bitmap.h $(CORETERM)
260-
$(CC) /c $(CFLAGS) $(TERMFLAGS) $(S)\$(*B).c /Fo$@
261+
$(CC) /c $(CFLAGS) $(WIN_CFLAGS) $(TERMFLAGS) $(S)\$(*B).c /Fo$@
261262

262263
term.cobj: $(S)\term.c $(S)\term.h $(S)\plot.h $(S)\setshow.h $(S)\bitmap.h $(CORETERM)
263264
$(CC) /c $(CFLAGS) $(CONSOLE_CFLAGS) $(TERMFLAGS) $(S)\$(*B).c /Fo$@
264265

265266
winmain.obj: $(W)winmain.c $(W)wgnuplib.h $(W)wtext.h $(S)\plot.h
266-
$(CC) /c $(CFLAGS) $(W)$(*B).c /Fo$@
267+
$(CC) /c $(CFLAGS) $(WIN_CFLAGS) $(W)$(*B).c /Fo$@
267268

268269
winmain.cobj: $(W)winmain.c $(W)wgnuplib.h $(W)wtext.h $(S)\plot.h
269270
$(CC) /c $(CFLAGS) $(CONSOLE_CFLAGS) $(W)winmain.c /Fo$@

‎src/win/winmain.c‎

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef lint
2-
static char *RCSid() { return RCSid("$Id: winmain.c,v 1.52 2011/11/14 21:03:38 markisch Exp $"); }
2+
static char *RCSid() { return RCSid("$Id: winmain.c,v 1.53 2011/12/05 19:25:10 markisch Exp $"); }
33
#endif
44

55
/* GNUPLOT - win/winmain.c */
@@ -829,6 +829,107 @@ MyFRead(void *ptr, size_t size, size_t n, FILE *file)
829829
return fread(ptr, size, n, file);
830830
}
831831

832+
833+
#ifdef USE_FAKEPIPES
834+
835+
static char pipe_type = NUL;
836+
static char * pipe_filename = NULL;
837+
static char * pipe_command = NULL;
838+
839+
FILE *
840+
fake_popen(const char * command, const char * type)
841+
{
842+
FILE * f = NULL;
843+
char tmppath[MAX_PATH];
844+
char tmpfile[MAX_PATH];
845+
DWORD ret;
846+
847+
if (type == NULL) return NULL;
848+
849+
pipe_type = NUL;
850+
if (pipe_filename != NULL)
851+
free(pipe_filename);
852+
853+
/* Random temp file name in %TEMP% */
854+
ret = GetTempPath(sizeof(tmppath), tmppath);
855+
if ((ret == 0) || (ret > sizeof(tmppath)))
856+
return NULL;
857+
ret = GetTempFileName(tmppath, "gpp", 0, tmpfile);
858+
if (ret == 0)
859+
return NULL;
860+
pipe_filename = strdup(tmpfile);
861+
862+
if (*type == 'r') {
863+
char * cmd;
864+
int rc;
865+
pipe_type = *type;
866+
/* Execute command with redirection of stdout to temporary file. */
867+
cmd = (char *) malloc(strlen(command) + strlen(pipe_filename) + 5);
868+
sprintf(cmd, "%s > %s", command, pipe_filename);
869+
rc = system(cmd);
870+
free(cmd);
871+
/* Now open temporary file. */
872+
/* system() returns 1 if the command could not be executed. */
873+
if (rc != 1)
874+
f = fopen(pipe_filename, "r");
875+
else {
876+
remove(pipe_filename);
877+
free(pipe_filename);
878+
pipe_filename = NULL;
879+
errno = EINVAL;
880+
}
881+
} else if (*type == 'w') {
882+
pipe_type = *type;
883+
/* Write output to temporary file and handle the rest in fake_pclose. */
884+
if (type[1] == 'b')
885+
int_error(NO_CARET, "Could not execute pipe '%s'. Writing to binary pipes is not supported.", command);
886+
else
887+
f = fopen(pipe_filename, "w");
888+
pipe_command = strdup(command);
889+
}
890+
891+
return f;
892+
}
893+
894+
895+
int fake_pclose(FILE *stream)
896+
{
897+
int rc = 0;
898+
if (!stream) return ECHILD;
899+
900+
/* Close temporary file */
901+
fclose(stream);
902+
903+
/* Finally, execute command with redirected stdin. */
904+
if (pipe_type == 'w') {
905+
char * cmd;
906+
cmd = (char *) malloc(strlen(pipe_command) + strlen(pipe_filename) + 10);
907+
/* FIXME: this won't work for binary data. We need a proper `cat` replacement. */
908+
sprintf(cmd, "type %s | %s", pipe_filename, pipe_command);
909+
rc = system(cmd);
910+
free(cmd);
911+
}
912+
913+
/* Delete temp file again. */
914+
if (pipe_filename) {
915+
remove(pipe_filename);
916+
errno = 0;
917+
free(pipe_filename);
918+
pipe_filename = NULL;
919+
}
920+
921+
if (pipe_command) {
922+
/* system() returns 255 if the command could not be executed.
923+
The real popen would have returned an error already. */
924+
if (rc == 255)
925+
int_error(NO_CARET, "Could not execute pipe '%s'.", pipe_command);
926+
free(pipe_command);
927+
}
928+
929+
return rc;
930+
}
931+
#endif
932+
832933
#else /* WGP_CONSOLE */
833934

834935
DWORD WINAPI stdin_pipe_reader(LPVOID param)

‎src/win/wtext.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $Id: wtext.h,v 1.9 2004/07/01 17:10:11 broeker Exp $
2+
* $Id: wtext.h,v 1.10 2008/11/07 11:55:46 mikulik Exp $
33
*/
44

55
/* GNUPLOT - win/wtext.h */
@@ -104,6 +104,11 @@ int MyPrintF(const char *fmt, ...);
104104
size_t MyFWrite(const void *ptr, size_t size, size_t n, FILE *stream);
105105
size_t MyFRead(void *ptr, size_t size, size_t n, FILE *stream);
106106

107+
#ifdef USE_FAKEPIPES
108+
FILE *fake_popen(const char *command, const char *type);
109+
int fake_pclose(FILE *stream);
110+
#endif
111+
107112
#else /* WGP_CONSOLE */
108113

109114
#define getch ConsoleGetch

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL