2 * gs interface for page.
3 * ps.c and pdf.c both use these routines.
4 * a caveat: if you run more than one gs, only the last
5 * one gets killed by killgs
14 static int gspid; /* globals for atexit */
16 static void killgs(void);
24 postnote(PNGROUP, getpid(), "die");
27 * from ghostscript's use.txt:
28 * ``Ghostscript currently doesn't do a very good job of deleting temporary
29 * files when it exits; you may have to delete them manually from time to
32 sprint(tmpfile, "/tmp/gs_%.5da", (gspid+300000)%100000);
33 if(chatty) fprint(2, "remove %s...\n", tmpfile);
36 postnote(PNPROC, gspid, "die yankee pig dog");
40 spawnwriter(GSInfo *g, Biobuf *b)
54 while((n = Bread(b, buf, sizeof buf)) > 0)
56 fprint(fd, "(/fd/3) (w) file dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring flushfile\n");
82 wexits("fork failed");
84 while((n=read(fd, buf, sizeof buf)) > 0) {
86 write(pfd[0], buf, n);
90 while((n=read(pfd[0], buf, sizeof buf)) > 0) {
96 postnote(PNGROUP, getpid(), "i'm die-ing");
110 switch(rfork(RFFDG|RFNOTEG|RFPROC)){
119 out = open("/dev/cons", OWRITE);
123 xbuf = buf; /* for ease of acid */
125 while((n = read(fd, xbuf, sizeof buf)) > 0){
128 fprint(2, "Ghostscript Error:\n");
150 * gs's standard input is fed from stdinout.
151 * gs output written to fd-2 (i.e. output we generate intentionally) is fed to stdinout.
152 * gs output written to fd 1 (i.e. ouptut gs generates on error) is fed to errout.
153 * gs data output is written to fd 3, which is dataout.
155 if(pipe(stdinout) < 0 || pipe(dataout)<0 || pipe(errout)<0)
159 args[nargs++] = "gs";
160 args[nargs++] = "-dNOPAUSE";
161 args[nargs++] = "-dSAFER";
162 args[nargs++] = "-sDEVICE=plan9";
163 args[nargs++] = "-sOutputFile=/fd/3";
164 args[nargs++] = "-dQUIET";
165 args[nargs++] = "-r100";
166 sprint(tb, "-dTextAlphaBits=%d", textbits);
167 sprint(gb, "-dGraphicsAlphaBits=%d", gfxbits);
182 * Horrible problem: we want to dup fd's 0-4 below,
183 * but some of the source fd's might have those small numbers.
184 * So we need to reallocate those. In order to not step on
185 * anything else, we'll dup the fd's to higher ones using
186 * dup(x, -1), but we need to use up the lower ones first.
188 while((devnull = open("/dev/null", ORDWR)) < 5)
191 stdinout[0] = dup(stdinout[0], -1);
192 errout[0] = dup(errout[0], -1);
193 dataout[0] = dup(dataout[0], -1);
197 dup(devnull, 2); /* never anything useful */
202 exec("/bin/gs", args);
211 stdinout[1] = spawnreader(stdinout[1]);
213 gsfd = g->gsfd = stdinout[1];
214 g->gsdfd = dataout[1];
217 spawnmonitor(errout[1]);
218 Binit(&g->gsrd, g->gsfd, OREAD);
220 gscmd(g, "/PAGEOUT (/fd/4) (w) file def\n");
221 gscmd(g, "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n");
228 gscmd(GSInfo *gs, char *fmt, ...)
235 n = vseprint(buf, buf+sizeof buf, fmt, v) - buf;
244 if(write(gs->gsfd, buf, n) != 0)
251 * set the dimensions of the bitmap we expect to get back from GS.
254 setdim(GSInfo *gs, Rectangle bbox, int ppi, int landscape)
259 fprint(2, "setdim: bbox=%R\n", bbox);
266 gscmd(gs, "/HWResolution [%d %d]\n", ppi, ppi);
269 bbox = Rect(0, 0, 612, 792); /* 8½×11 */
276 pbox = Rect(bbox.min.y, bbox.min.x, bbox.max.y, bbox.max.x);
279 gscmd(gs, "/PageSize [%d %d]\n", Dx(pbox), Dy(pbox));
280 gscmd(gs, "/Margins [%d %d]\n", -pbox.min.x, -pbox.min.y);
281 gscmd(gs, "currentdevice putdeviceprops pop\n");
282 gscmd(gs, "/#copies 1 store\n");
284 if(!eqpt(bbox.min, ZP))
285 gscmd(gs, "%d %d translate\n", -bbox.min.x, -bbox.min.y);
291 gscmd(gs, "%d 0 translate\n", Dy(bbox));
292 gscmd(gs, "90 rotate\n");
302 /* we figure out that gs is done by telling it to
303 * print something and waiting until it does.
306 Biobuf *b = &gs->gsrd;
310 // gscmd(gs, "(\\n**bstack\\n) print flush\n");
311 // gscmd(gs, "stack flush\n");
312 // gscmd(gs, "(**estack\\n) print flush\n");
313 gscmd(gs, "(\\n//GO.SYSIN DD\\n) PAGE==\n");
317 p = Brdline(b, '\n');
327 p[Blinelen(b)-1] = 0;
328 if(chatty) fprint(2, "p: ");
329 if(chatty) write(2, p, Blinelen(b)-1);
330 if(chatty) fprint(2, "\n");
331 if(strstr(p, "Error:")) {
333 fprint(2, "ghostscript error: %s\n", p);
337 if(strstr(p, "//GO.SYSIN DD")) {