summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipng/dvipng-1.12-PATCHES/patch-11-win32-fork-and-pipe
blob: 89e88605690c219da1b00dde33042f6abd6fe6d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
diff -ur dvipng-1.12.orig/ChangeLog dvipng-1.12/ChangeLog
--- dvipng-1.12.orig/ChangeLog	2010-02-21 19:15:27.000000000 +0100
+++ dvipng-1.12/ChangeLog	2010-02-24 17:29:02.816848864 +0100
@@ -1,3 +1,15 @@
+2010-02-24  Peter Breitenlohner  <peb@mppmu.mpg.de>
+
+	Support WIN32 builds (native or MinGW32).
+	* dvipng.h (min, max): Always undefine before defining.
+	* dvi.c: For WIN32 use Sleep(1000) instead of sleeep(1).
+	* misc.c: #include <sys/mman.h> depends on HAVE_MMAP.
+	* dvipng.h, font.c, misc.c: For mmap code use '#ifdef WIN32'
+	instead of '#ifdef MIKTEX'.
+
+	* special.c: WIN32 specific replacement for fork and pipe, using
+	code from Akira Kakuto, (e-mail from 23 Feb 2010 23:33:58).
+
 2010-01-29  Karl Berry  <karl@tug.org>
 
 	* test_dvipng.tex: Do not use \usepackage{mathptmx}.
diff -ur dvipng-1.12.orig/special.c dvipng-1.12/special.c
--- dvipng-1.12.orig/special.c	2009-10-01 14:43:06.000000000 +0200
+++ dvipng-1.12/special.c	2010-02-24 16:10:10.080005933 +0100
@@ -24,6 +24,15 @@
 
 #include "dvipng.h"
 
+#ifndef MIKTEX
+#ifdef WIN32
+#include <fcntl.h>
+#include <io.h>
+#include <process.h>
+#define snprintf _snprintf
+#endif /* WIN32 */
+#endif
+
 #define SKIPSPACES(s) while(s && *s==' ' && *s!='\0') s++
 
 /* PostScript can come as a string (headers and raw specials) or 
@@ -132,13 +141,19 @@
 
 
 static gdImagePtr
-ps2png(struct pscode* pscodep, const char *device, int hresolution, int vresolution, 
+ps2png(struct pscode* pscodep, const char *device, int hresolution, int vresolution,
        int llx, int lly, int urx, int ury, int bgred, int bggreen, int bgblue)
 {
 #ifndef MIKTEX
   int downpipe[2], uppipe[2];
+#ifdef WIN32
+  int nexitcode = STILL_ACTIVE;
+  HANDLE hchild;
+  int savestdin, savestdout;
+#else /* !WIN32 */
   pid_t pid;
-#else
+#endif /* !WIN32 */
+#else /* MIKTEX */
   HANDLE hPngStream;
   HANDLE hPsStream;
   HANDLE hStdErr;
@@ -147,7 +162,7 @@
   _TCHAR szGsPath[_MAX_PATH];
 #define GS_PATH szGsPath
   int fd;
-#endif
+#endif /* MIKTEX */
   FILE *psstream=NULL, *pngstream=NULL;
   char resolution[STRSIZE]; 
   /*   char devicesize[STRSIZE];  */
@@ -170,6 +185,7 @@
 	       (option_flags & NO_GSSAFER) ? "-": "-dSAFER", 
 	       (option_flags & NO_GSSAFER) ? "": "- "));
 #ifndef MIKTEX
+#ifndef WIN32
   if (pipe(downpipe) || pipe(uppipe)) return(NULL);
   /* Ready to fork */
   pid = fork ();
@@ -187,18 +203,51 @@
 	   (option_flags & NO_GSSAFER) ? NULL: "-",
 	   NULL);
     _exit (EXIT_FAILURE);
+#else /* WIN32 */
+  if (_pipe(downpipe, 65536, O_BINARY | _O_NOINHERIT)==-1 ||
+      _pipe(uppipe, 65536, O_BINARY | _O_NOINHERIT)==-1) {
+     fprintf(stderr, "Pipe error.\n");
+     return NULL;
+#endif /* WIN32 */
   }
   /* Parent process. */
-  
+#ifdef WIN32
+  savestdin = _dup(fileno(stdin));
+  _dup2(downpipe[0], fileno(stdin));
+#endif /* WIN32 */
   close(downpipe[0]);
+#ifdef WIN32
+  savestdout = _dup(fileno(stdout));
+  _dup2(uppipe[1], fileno(stdout));
+  close(uppipe[1]);
+#endif /* WIN32 */
   psstream=fdopen(downpipe[1],"wb");
   /* fclose(psstream);  psstream=fopen("test.ps","wb"); */
+#ifndef WIN32
   if (psstream == NULL) 
     close(downpipe[1]);
   close(uppipe[1]);
   pngstream=fdopen(uppipe[0],"rb");
   if (pngstream == NULL) 
     close(uppipe[0]);
+#else /* WIN32 */
+  if (psstream == NULL) {
+     fprintf(stderr, "psstream == NULL\n");
+     close(downpipe[1]);
+  }
+  pngstream=fdopen(uppipe[0],"rb");
+  if (pngstream == NULL) {
+     fprintf(stderr, "pngstream == NULL\n");
+     close(uppipe[0]);
+  }
+  hchild=(HANDLE)spawnlp(_P_NOWAIT, GS_PATH, GS_PATH, device, resolution,
+         "-dBATCH", "-dNOPAUSE", "-q", "-sOutputFile=-", 
+         "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4",
+         (option_flags & NO_GSSAFER) ? "-": "-dSAFER", 
+         (option_flags & NO_GSSAFER) ? NULL : "-", NULL);
+
+  if(hchild) {
+#endif /* WIN32 */
 #else /* MIKTEX */
   if (! miktex_find_miktex_executable("mgs.exe", szGsPath)) {
       Warning("Ghostscript could not be found");
@@ -228,7 +277,7 @@
     if (pngstream == NULL) 
       _close (fd);
   }
-#endif 
+#endif /* MIKTEX */
   if (psstream) {
     writepscode(psheaderp,psstream);
     DEBUG_PRINT(DEBUG_GS,("\n  PS CODE:\t<</PageSize[%d %d]/PageOffset[%d %d[1 1 dtransform exch]{0 ge{neg}if exch}forall]>>setpagedevice",
@@ -254,7 +303,24 @@
   }
 #ifdef MIKTEX
   CloseHandle(pi.hProcess);
-#endif
+#else /* !MIKTEX */
+#ifdef WIN32
+  }
+  while(nexitcode == STILL_ACTIVE)
+    GetExitCodeProcess((HANDLE)hchild, (unsigned long*)&nexitcode);
+
+  CloseHandle((HANDLE)hchild);
+  _dup2(savestdin, fileno(stdin));
+  _dup2(savestdout, fileno(stdout));
+  close(savestdin);
+  close(savestdout);
+  close(uppipe[0]);
+  close(uppipe[1]);
+  close(downpipe[0]);
+  close(downpipe[1]);
+#endif /* WIN32 */
+#endif /* !MIKTEX */
+
   if (psimage == NULL) {
     DEBUG_PRINT(DEBUG_GS,("\n  GS OUTPUT:\tNO IMAGE "));
     if (!showpage) {