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
15 static int gspid; /* globals for atexit */
17 static void killgs(void);
25 postnote(PNGROUP, getpid(), "die");
28 * from ghostscript's use.txt:
29 * ``Ghostscript currently doesn't do a very good job of deleting temporary
30 * files when it exits; you may have to delete them manually from time to
33 sprint(tmpfile, "/tmp/gs_%.5da", (gspid+300000)%100000);
34 if(chatty) fprint(2, "remove %s...\n", tmpfile);
37 postnote(PNPROC, gspid, "die yankee pig dog");
41 spawnwriter(GSInfo *g, Biobuf *b)
55 while((n = Bread(b, buf, sizeof buf)) > 0)
57 fprint(fd, "(/fd/3) (w) file dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring flushfile\n");
83 wexits("fork failed");
85 while((n=read(fd, buf, sizeof buf)) > 0) {
87 write(pfd[0], buf, n);
91 while((n=read(pfd[0], buf, sizeof buf)) > 0) {
97 postnote(PNGROUP, getpid(), "i'm die-ing");
111 switch(rfork(RFFDG|RFNOTEG|RFPROC)){
120 out = open("/dev/cons", OWRITE);
124 xbuf = buf; /* for ease of acid */
126 while((n = read(fd, xbuf, sizeof buf)) > 0){
129 fprint(2, "Ghostscript Error:\n");
138 spawngs(GSInfo *g, char *safer)
151 * gs's standard input is fed from stdinout.
152 * gs output written to fd-2 (i.e. output we generate intentionally) is fed to stdinout.
153 * gs output written to fd 1 (i.e. ouptut gs generates on error) is fed to errout.
154 * gs data output is written to fd 3, which is dataout.
156 if(pipe(stdinout) < 0 || pipe(dataout)<0 || pipe(errout)<0)
160 args[nargs++] = "gs";
161 args[nargs++] = "-dNOPAUSE";
162 args[nargs++] = safer;
163 args[nargs++] = "-sDEVICE=plan9";
164 args[nargs++] = "-sOutputFile=/fd/3";
165 args[nargs++] = "-dQUIET";
166 args[nargs++] = "-r100";
167 sprint(tb, "-dTextAlphaBits=%d", textbits);
168 sprint(gb, "-dGraphicsAlphaBits=%d", gfxbits);
183 * Horrible problem: we want to dup fd's 0-4 below,
184 * but some of the source fd's might have those small numbers.
185 * So we need to reallocate those. In order to not step on
186 * anything else, we'll dup the fd's to higher ones using
187 * dup(x, -1), but we need to use up the lower ones first.
189 while((devnull = open("/dev/null", ORDWR)) < 5)
192 stdinout[0] = dup(stdinout[0], -1);
193 errout[0] = dup(errout[0], -1);
194 dataout[0] = dup(dataout[0], -1);
198 dup(devnull, 2); /* never anything useful */
203 exec("/bin/gs", args);
212 stdinout[1] = spawnreader(stdinout[1]);
214 gsfd = g->gsfd = stdinout[1];
215 g->gsdfd = dataout[1];
218 spawnmonitor(errout[1]);
219 Binit(&g->gsrd, g->gsfd, OREAD);
221 gscmd(g, "/PAGEOUT (/fd/4) (w) file def\n");
222 gscmd(g, "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n");
229 gscmd(GSInfo *gs, char *fmt, ...)
236 n = vseprint(buf, buf+sizeof buf, fmt, v) - buf;
245 if(write(gs->gsfd, buf, n) != 0)
252 * set the dimensions of the bitmap we expect to get back from GS.
255 setdim(GSInfo *gs, Rectangle bbox, int ppi, int landscape)
260 fprint(2, "setdim: bbox=%R\n", bbox);
267 gscmd(gs, "/HWResolution [%d %d]\n", ppi, ppi);
270 bbox = Rect(0, 0, 612, 792); /* 8½×11 */
273 pbox = Rect(bbox.min.y, bbox.min.x, bbox.max.y, bbox.max.x);
277 gscmd(gs, "/PageSize [%d %d]\n", Dx(pbox), Dy(pbox));
278 gscmd(gs, "/Margins [%d %d]\n", -pbox.min.x, -pbox.min.y);
279 gscmd(gs, "currentdevice putdeviceprops pop\n");
280 gscmd(gs, "/#copies 1 store\n");
282 if(!eqpt(bbox.min, ZP))
283 gscmd(gs, "%d %d translate\n", -bbox.min.x, -bbox.min.y);
289 gscmd(gs, "%d 0 translate\n", Dy(bbox));
290 gscmd(gs, "90 rotate\n");
300 /* we figure out that gs is done by telling it to
301 * print something and waiting until it does.
304 Biobuf *b = &gs->gsrd;
308 /* gscmd(gs, "(\\n**bstack\\n) print flush\n"); */
309 /* gscmd(gs, "stack flush\n"); */
310 /* gscmd(gs, "(**estack\\n) print flush\n"); */
311 gscmd(gs, "(\\n/*GO.SYSIN DD\\n) PAGE==\n"); */
315 p = Brdline(b, '\n');
325 p[Blinelen(b)-1] = 0;
326 if(chatty) fprint(2, "p: ");
327 if(chatty) write(2, p, Blinelen(b)-1);
328 if(chatty) fprint(2, "\n");
329 if(strstr(p, "Error:")) {
331 fprint(2, "ghostscript error: %s\n", p);
335 if(strstr(p, "//GO.SYSIN DD")) {