+ p = (struct termp *)arg;
+
+ if (p->engine.ps.psmarg)
+ free(p->engine.ps.psmarg);
+ if (p->engine.ps.pdfobjs)
+ free(p->engine.ps.pdfobjs);
+
+ term_free(p);
+}
+
+
+static void
+ps_printf(struct termp *p, const char *fmt, ...)
+{
+ va_list ap;
+ int pos, len;
+
+ va_start(ap, fmt);
+
+ /*
+ * If we're running in regular mode, then pipe directly into
+ * vprintf(). If we're processing margins, then push the data
+ * into our growable margin buffer.
+ */
+
+ if ( ! (PS_MARGINS & p->engine.ps.flags)) {
+ len = vprintf(fmt, ap);
+ va_end(ap);
+ p->engine.ps.pdfbytes += /* LINTED */
+ len < 0 ? 0 : (size_t)len;
+ return;
+ }
+
+ /*
+ * XXX: I assume that the in-margin print won't exceed
+ * PS_BUFSLOP (128 bytes), which is reasonable but still an
+ * assumption that will cause pukeage if it's not the case.
+ */
+
+ ps_growbuf(p, PS_BUFSLOP);
+
+ pos = (int)p->engine.ps.psmargcur;
+ len = vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
+
+ va_end(ap);
+
+ p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
+}
+
+
+static void
+ps_putchar(struct termp *p, char c)
+{
+ int pos;
+
+ /* See ps_printf(). */
+
+ if ( ! (PS_MARGINS & p->engine.ps.flags)) {
+ putchar(c);
+ p->engine.ps.pdfbytes++;
+ return;
+ }
+
+ ps_growbuf(p, 2);
+
+ pos = (int)p->engine.ps.psmargcur++;
+ p->engine.ps.psmarg[pos++] = c;
+ p->engine.ps.psmarg[pos] = '\0';
+}
+
+
+static void
+pdf_obj(struct termp *p, size_t obj)
+{
+
+ assert(obj > 0);
+
+ if ((obj - 1) >= p->engine.ps.pdfobjsz) {
+ p->engine.ps.pdfobjsz = obj + 128;
+ p->engine.ps.pdfobjs = realloc
+ (p->engine.ps.pdfobjs,
+ p->engine.ps.pdfobjsz * sizeof(size_t));
+ if (NULL == p->engine.ps.pdfobjs) {
+ perror(NULL);
+ exit(MANDOCLEVEL_SYSERR);
+ }
+ }
+
+ p->engine.ps.pdfobjs[(int)obj - 1] = p->engine.ps.pdfbytes;
+ ps_printf(p, "%zu 0 obj\n", obj);
+}
+
+
+static void
+ps_closepage(struct termp *p)
+{
+ int i;
+ size_t len, base;
+
+ /*
+ * Close out a page that we've already flushed to output. In
+ * PostScript, we simply note that the page must be showed. In
+ * PDF, we must now create the Length, Resource, and Page node
+ * for the page contents.
+ */
+
+ assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
+ ps_printf(p, "%s", p->engine.ps.psmarg);
+
+ if (TERMTYPE_PS != p->type) {
+ ps_printf(p, "ET\n");
+
+ len = p->engine.ps.pdfbytes - p->engine.ps.pdflastpg;
+ base = p->engine.ps.pages * 4 + p->engine.ps.pdfbody;
+
+ ps_printf(p, "endstream\nendobj\n");
+
+ /* Length of content. */
+ pdf_obj(p, base + 1);
+ ps_printf(p, "%zu\nendobj\n", len);
+
+ /* Resource for content. */
+ pdf_obj(p, base + 2);
+ ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
+ ps_printf(p, "/Font <<\n");
+ for (i = 0; i < (int)TERMFONT__MAX; i++)
+ ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
+ ps_printf(p, ">>\n>>\n");
+
+ /* Page node. */
+ pdf_obj(p, base + 3);
+ ps_printf(p, "<<\n");
+ ps_printf(p, "/Type /Page\n");
+ ps_printf(p, "/Parent 2 0 R\n");
+ ps_printf(p, "/Resources %zu 0 R\n", base + 2);
+ ps_printf(p, "/Contents %zu 0 R\n", base);
+ ps_printf(p, ">>\nendobj\n");
+ } else
+ ps_printf(p, "showpage\n");
+
+ p->engine.ps.pages++;
+ p->engine.ps.psrow = p->engine.ps.top;
+ assert( ! (PS_NEWPAGE & p->engine.ps.flags));
+ p->engine.ps.flags |= PS_NEWPAGE;