summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2010-06-07 20:57:09 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2010-06-07 20:57:09 +0000
commit7506d04e28e759651e3b7cf0f671282cfb8ab4fd (patch)
treedcc0c1b0d2b0b05ca3f50a2766c7e15cd07d3718
parent1ce42efc4cb4b55e9e64a347176288d71b9d5cc5 (diff)
downloadmandoc-7506d04e28e759651e3b7cf0f671282cfb8ab4fd.tar.gz
First check-in of PostScript output. This does not change any logic
within term.c, but does add a small shim over putchar() that switches on the output engine. Prints, for this initial version, only monospace and without font decorations. It's a start.
-rw-r--r--Makefile22
-rw-r--r--main.c32
-rw-r--r--main.h1
-rw-r--r--man_term.c22
-rw-r--r--mandoc.19
-rw-r--r--mdoc_term.c20
-rw-r--r--term.c254
-rw-r--r--term.h22
8 files changed, 332 insertions, 50 deletions
diff --git a/Makefile b/Makefile
index 5bc86271..3ff9892b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-.SUFFIXES: .html .xml .sgml .1 .3 .7 .md5 .tar.gz .1.txt .3.txt .7.txt .1.sgml .3.sgml .7.sgml .h .h.html
+.SUFFIXES: .html .xml .sgml .1 .3 .7 .md5 .tar.gz .1.txt .3.txt .7.txt .1.sgml .3.sgml .7.sgml .h .h.html .1.ps .3.ps .7.ps
PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
@@ -33,6 +33,8 @@ MANDOCFLAGS = -Wall -fstrict
MANDOCHTML = -Thtml -Ostyle=style.css,man=%N.%S.html,includes=%I.html
+MANDOCPS = -Tps
+
ROFFLNS = roff.ln
ROFFSRCS = roff.c
@@ -107,6 +109,9 @@ HTMLS = ChangeLog.html index.html man.h.html mdoc.h.html \
man.3.html mdoc.7.html man.7.html mandoc_char.7.html \
roff.7.html roff.3.html
+PSS = mandoc.1.ps mdoc.3.ps man.3.ps mdoc.7.ps man.7.ps \
+ mandoc_char.7.ps roff.7.ps roff.3.ps
+
XSLS = ChangeLog.xsl
TEXTS = mandoc.1.txt mdoc.3.txt man.3.txt mdoc.7.txt man.7.txt \
@@ -134,7 +139,7 @@ CONFIGS = config.h.pre config.h.post
DOCLEAN = $(BINS) $(LNS) $(LLNS) $(LIBS) $(OBJS) $(HTMLS) \
$(TARGZS) tags $(MD5S) $(XMLS) $(TEXTS) $(GSGMLS) \
- config.h config.log
+ config.h config.log $(PSS)
DOINSTALL = $(SRCS) $(HEADS) Makefile $(MANS) $(SGMLS) $(STATICS) \
$(DATAS) $(XSLS) $(EXAMPLES) $(TESTS) $(CONFIGS)
@@ -146,15 +151,11 @@ lint: $(LLNS)
clean:
rm -f $(DOCLEAN)
-cleanlint:
- rm -f $(LNS) $(LLNS)
-
-cleanhtml:
- rm -f $(HTMLS) $(GSGMLS)
-
dist: mdocml-$(VERSION).tar.gz
-www: all $(GSGMLS) $(HTMLS) $(TEXTS) $(MD5S) $(TARGZS)
+www: all $(GSGMLS) $(HTMLS) $(TEXTS) $(MD5S) $(TARGZS) $(PSS)
+
+ps: $(PSS)
installwww: www
$(INSTALL_DATA) $(HTMLS) $(TEXTS) $(STATICS) $(DESTDIR)$(PREFIX)/
@@ -301,6 +302,9 @@ mandoc: $(MAINOBJS) libroff.a libmdoc.a libman.a libmandoc.a
.1.1.sgml .3.3.sgml .7.7.sgml:
./mandoc $(MANDOCFLAGS) $(MANDOCHTML) $< > $@
+.1.1.ps .3.3.ps .7.7.ps:
+ ./mandoc $(MANDOCFLAGS) $(MANDOCPS) $< > $@
+
.tar.gz.md5:
md5 $< > $@
diff --git a/main.c b/main.c
index e27e2bf1..19c47a25 100644
--- a/main.c
+++ b/main.c
@@ -65,7 +65,8 @@ enum outt {
OUTT_TREE,
OUTT_HTML,
OUTT_XHTML,
- OUTT_LINT
+ OUTT_LINT,
+ OUTT_PS
};
struct curparse {
@@ -584,12 +585,24 @@ fdesc(struct curparse *curp)
switch (curp->outtype) {
case (OUTT_XHTML):
curp->outdata = xhtml_alloc(curp->outopts);
- curp->outman = html_man;
- curp->outmdoc = html_mdoc;
- curp->outfree = html_free;
break;
case (OUTT_HTML):
curp->outdata = html_alloc(curp->outopts);
+ break;
+ case (OUTT_ASCII):
+ curp->outdata = ascii_alloc(curp->outopts);
+ break;
+ case (OUTT_PS):
+ curp->outdata = ps_alloc();
+ break;
+ default:
+ break;
+ }
+
+ switch (curp->outtype) {
+ case (OUTT_HTML):
+ /* FALLTHROUGH */
+ case (OUTT_XHTML):
curp->outman = html_man;
curp->outmdoc = html_mdoc;
curp->outfree = html_free;
@@ -598,14 +611,15 @@ fdesc(struct curparse *curp)
curp->outman = tree_man;
curp->outmdoc = tree_mdoc;
break;
- case (OUTT_LINT):
- break;
- default:
- curp->outdata = ascii_alloc(curp->outopts);
+ case (OUTT_ASCII):
+ /* FALLTHROUGH */
+ case (OUTT_PS):
curp->outman = terminal_man;
curp->outmdoc = terminal_mdoc;
curp->outfree = terminal_free;
break;
+ default:
+ break;
}
}
@@ -729,6 +743,8 @@ toptions(struct curparse *curp, char *arg)
curp->outtype = OUTT_HTML;
else if (0 == strcmp(arg, "xhtml"))
curp->outtype = OUTT_XHTML;
+ else if (0 == strcmp(arg, "ps"))
+ curp->outtype = OUTT_PS;
else {
fprintf(stderr, "%s: Bad argument\n", arg);
return(0);
diff --git a/main.h b/main.h
index 45c092e9..49083fe3 100644
--- a/main.h
+++ b/main.h
@@ -42,6 +42,7 @@ void tree_mdoc(void *, const struct mdoc *);
void tree_man(void *, const struct man *);
void *ascii_alloc(char *);
+void *ps_alloc(void);
void terminal_mdoc(void *, const struct mdoc *);
void terminal_man(void *, const struct man *);
void terminal_free(void *);
diff --git a/man_term.c b/man_term.c
index 2d82a83b..b5497e50 100644
--- a/man_term.c
+++ b/man_term.c
@@ -73,12 +73,10 @@ struct termact {
static int a2width(const struct man_node *);
static int a2height(const struct man_node *);
-static void print_man_head(struct termp *,
- const struct man_meta *);
+static void print_man_head(struct termp *, const void *);
static void print_man_nodelist(DECL_ARGS);
static void print_man_node(DECL_ARGS);
-static void print_man_foot(struct termp *,
- const struct man_meta *);
+static void print_man_foot(struct termp *, const void *);
static void print_bvspace(struct termp *,
const struct man_node *);
@@ -161,6 +159,8 @@ terminal_man(void *arg, const struct man *man)
p->overstep = 0;
p->maxrmargin = p->defrmargin;
+ term_begin(p, print_man_head, print_man_foot, man_meta(man));
+
if (NULL == p->symtab)
switch (p->enc) {
case (TERMENC_ASCII):
@@ -174,7 +174,6 @@ terminal_man(void *arg, const struct man *man)
n = man_node(man);
m = man_meta(man);
- print_man_head(p, m);
p->flags |= TERMP_NOSPACE;
mt.fl = 0;
@@ -183,7 +182,8 @@ terminal_man(void *arg, const struct man *man)
if (n->child)
print_man_nodelist(p, &mt, n->child, m);
- print_man_foot(p, m);
+
+ term_end(p);
}
@@ -858,9 +858,12 @@ print_man_nodelist(DECL_ARGS)
static void
-print_man_foot(struct termp *p, const struct man_meta *meta)
+print_man_foot(struct termp *p, const void *arg)
{
char buf[DATESIZ];
+ const struct man_meta *meta;
+
+ meta = (const struct man_meta *)arg;
term_fontrepl(p, TERMFONT_NONE);
@@ -894,10 +897,13 @@ print_man_foot(struct termp *p, const struct man_meta *meta)
static void
-print_man_head(struct termp *p, const struct man_meta *m)
+print_man_head(struct termp *p, const void *arg)
{
char buf[BUFSIZ], title[BUFSIZ];
size_t buflen, titlen;
+ const struct man_meta *m;
+
+ m = (const struct man_meta *)arg;
/*
* Note that old groff would spit out some spaces before the
diff --git a/mandoc.1 b/mandoc.1
index d4d316b7..d32c2592 100644
--- a/mandoc.1
+++ b/mandoc.1
@@ -163,6 +163,10 @@ Implies
.Fl W Ns Cm all
and
.Fl f Ns Cm strict .
+.It Fl T Ns Cm ps
+Produce PostScript output.
+See
+.Sx PostScript Output .
.It Fl T Ns Cm tree
Produce an indented parse tree.
.It Fl T Ns Cm xhtml
@@ -343,6 +347,11 @@ cause rendered documents to appear as they do in
.Fl T Ns Cm ascii .
.Pp
Special characters are rendered in decimal-encoded UTF-8.
+.Ss PostScript Output
+PostScript Level 1 pages may be generated by
+.Fl T Ns Cm ps .
+Output pages are US-letter sized (215.9 x 279.4 mm) and rendered in
+fixed, 10-point Courier font.
.Ss XHTML Output
Output produced by
.Fl T Ns Cm xhtml
diff --git a/mdoc_term.c b/mdoc_term.c
index 84583caf..86a7fdde 100644
--- a/mdoc_term.c
+++ b/mdoc_term.c
@@ -65,9 +65,9 @@ static void print_bvspace(struct termp *,
const struct mdoc_node *,
const struct mdoc_node *);
static void print_mdoc_node(DECL_ARGS);
-static void print_mdoc_head(DECL_ARGS);
+static void print_mdoc_head(struct termp *, const void *);
static void print_mdoc_nodelist(DECL_ARGS);
-static void print_foot(DECL_ARGS);
+static void print_foot(struct termp *, const void *);
static void synopsis_pre(struct termp *,
const struct mdoc_node *);
@@ -276,6 +276,8 @@ terminal_mdoc(void *arg, const struct mdoc *mdoc)
p->maxrmargin = p->defrmargin;
p->tabwidth = 5;
+ term_begin(p, print_mdoc_head, print_foot, mdoc_meta(mdoc));
+
if (NULL == p->symtab)
switch (p->enc) {
case (TERMENC_ASCII):
@@ -289,10 +291,10 @@ terminal_mdoc(void *arg, const struct mdoc *mdoc)
n = mdoc_node(mdoc);
m = mdoc_meta(mdoc);
- print_mdoc_head(p, NULL, m, n);
if (n->child)
print_mdoc_nodelist(p, NULL, m, n->child);
- print_foot(p, NULL, m, n);
+
+ term_end(p);
}
@@ -348,9 +350,12 @@ print_mdoc_node(DECL_ARGS)
/* ARGSUSED */
static void
-print_foot(DECL_ARGS)
+print_foot(struct termp *p, const void *arg)
{
char buf[DATESIZ], os[BUFSIZ];
+ const struct mdoc_meta *m;
+
+ m = (const struct mdoc_meta *)arg;
term_fontrepl(p, TERMFONT_NONE);
@@ -397,9 +402,12 @@ print_foot(DECL_ARGS)
/* ARGSUSED */
static void
-print_mdoc_head(DECL_ARGS)
+print_mdoc_head(struct termp *p, const void *arg)
{
char buf[BUFSIZ], title[BUFSIZ];
+ const struct mdoc_meta *m;
+
+ m = (const struct mdoc_meta *)arg;
p->rmargin = p->maxrmargin;
p->offset = 0;
diff --git a/term.c b/term.c
index 9506101c..42f0c8e1 100644
--- a/term.c
+++ b/term.c
@@ -36,7 +36,15 @@
#include "mdoc.h"
#include "main.h"
-static struct termp *term_alloc(char *, enum termenc);
+#define PS_CHAR_WIDTH 6
+#define PS_CHAR_HEIGHT 12
+#define PS_CHAR_TOPMARG (792 - 24)
+#define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
+#define PS_CHAR_LEFT 36
+#define PS_CHAR_BOTMARG 24
+#define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
+
+static struct termp *alloc(char *, enum termenc, enum termtype);
static void term_free(struct termp *);
static void spec(struct termp *, const char *, size_t);
static void res(struct termp *, const char *, size_t);
@@ -44,13 +52,25 @@ static void buffera(struct termp *, const char *, size_t);
static void bufferc(struct termp *, char);
static void adjbuf(struct termp *p, size_t);
static void encode(struct termp *, const char *, size_t);
+static void advance(struct termp *, size_t);
+static void endline(struct termp *);
+static void letter(struct termp *, char);
+static void pageopen(struct termp *);
void *
ascii_alloc(char *outopts)
{
- return(term_alloc(outopts, TERMENC_ASCII));
+ return(alloc(outopts, TERMENC_ASCII, TERMTYPE_CHAR));
+}
+
+
+void *
+ps_alloc(void)
+{
+
+ return(alloc(NULL, TERMENC_ASCII, TERMTYPE_PS));
}
@@ -70,13 +90,207 @@ term_free(struct termp *p)
free(p->buf);
if (p->symtab)
chars_free(p->symtab);
-
free(p);
}
+/*
+ * Push a single letter into our output engine.
+ */
+static void
+letter(struct termp *p, char c)
+{
+
+ if (TERMTYPE_CHAR == p->type) {
+ /*
+ * If using the terminal device, just push the letter
+ * out into the screen.
+ */
+ putchar(c);
+ return;
+ }
+
+ if ( ! (PS_INLINE & p->psstate)) {
+ /*
+ * If we're not in a PostScript "word" context, then
+ * open one now at the current cursor.
+ */
+ printf("%zu %zu moveto\n", p->pscol, p->psrow);
+ putchar('(');
+ p->psstate |= PS_INLINE;
+ }
+
+ /*
+ * We need to escape these characters as per the PostScript
+ * specification. We would also escape non-graphable characters
+ * (like tabs), but none of them would get to this point and
+ * it's superfluous to abort() on them.
+ */
+
+ switch (c) {
+ case ('('):
+ /* FALLTHROUGH */
+ case (')'):
+ /* FALLTHROUGH */
+ case ('\\'):
+ putchar('\\');
+ break;
+ default:
+ break;
+ }
+
+ /* Write the character and adjust where we are on the page. */
+ putchar(c);
+ p->pscol += PS_CHAR_WIDTH;
+}
+
+
+/*
+ * Begin a "terminal" context. Since terminal encompasses PostScript,
+ * the actual terminal, etc., there are a few things we can do here.
+ */
+void
+term_begin(struct termp *p, term_margin head,
+ term_margin foot, const void *arg)
+{
+
+ p->headf = head;
+ p->footf = foot;
+ p->argf = arg;
+
+ if (TERMTYPE_CHAR == p->type) {
+ /* Emit the header and be done. */
+ (*p->headf)(p, p->argf);
+ return;
+ }
+
+ /*
+ * Emit the standard PostScript prologue, set our initial page
+ * position, then run pageopen() on the initial page.
+ */
+
+ printf("%s\n", "%!PS");
+ printf("%s\n", "/Courier");
+ printf("%s\n", "10 selectfont");
+
+ p->pspage = 1;
+ p->psstate = 0;
+ pageopen(p);
+}
+
+
+/*
+ * Open a page. This is only used for -Tps at the moment. It opens a
+ * page context, printing the header and the footer. THE OUTPUT BUFFER
+ * MUST BE EMPTY. If it is not, output will ghost on the next line and
+ * we'll be all gross and out of state.
+ */
+static void
+pageopen(struct termp *p)
+{
+
+ assert(TERMTYPE_PS == p->type);
+ assert(0 == p->psstate);
+
+ p->pscol = PS_CHAR_LEFT;
+ p->psrow = PS_CHAR_TOPMARG;
+ p->psstate |= PS_MARGINS;
+
+ (*p->headf)(p, p->argf);
+ endline(p);
+
+ p->psstate &= ~PS_MARGINS;
+ assert(0 == p->psstate);
+
+ p->pscol = PS_CHAR_LEFT;
+ p->psrow = PS_CHAR_BOTMARG;
+ p->psstate |= PS_MARGINS;
+
+ (*p->footf)(p, p->argf);
+ endline(p);
+
+ p->psstate &= ~PS_MARGINS;
+ assert(0 == p->psstate);
+
+ p->pscol = PS_CHAR_LEFT;
+ p->psrow = PS_CHAR_TOP;
+
+}
+
+
+void
+term_end(struct termp *p)
+{
+
+ if (TERMTYPE_CHAR == p->type) {
+ (*p->footf)(p, p->argf);
+ return;
+ }
+
+ printf("%s\n", "%%END");
+}
+
+
+static void
+endline(struct termp *p)
+{
+
+ if (TERMTYPE_CHAR == p->type) {
+ putchar('\n');
+ return;
+ }
+
+ if (PS_INLINE & p->psstate) {
+ printf(") show\n");
+ p->psstate &= ~PS_INLINE;
+ }
+
+ if (PS_MARGINS & p->psstate)
+ return;
+
+ p->pscol = PS_CHAR_LEFT;
+ if (p->psrow >= PS_CHAR_HEIGHT + PS_CHAR_BOT) {
+ p->psrow -= PS_CHAR_HEIGHT;
+ return;
+ }
+
+ /*
+ * XXX: can't run pageopen() until we're certain a flushln() has
+ * occured, else the buf will reopen in an awkward state on the
+ * next line.
+ */
+ printf("showpage\n");
+ p->psrow = PS_CHAR_TOP;
+}
+
+
+/*
+ * Advance the output engine by a certain amount of whitespace.
+ */
+static void
+advance(struct termp *p, size_t len)
+{
+ size_t i;
+
+ if (TERMTYPE_CHAR == p->type) {
+ /* Just print whitespace on the terminal. */
+ for (i = 0; i < len; i++)
+ putchar(' ');
+ return;
+ }
+
+ if (PS_INLINE & p->psstate) {
+ /* Dump out any existing line scope. */
+ printf(") show\n");
+ p->psstate &= ~PS_INLINE;
+ }
+
+ p->pscol += len ? len * PS_CHAR_WIDTH : 0;
+}
+
+
static struct termp *
-term_alloc(char *outopts, enum termenc enc)
+alloc(char *outopts, enum termenc enc, enum termtype type)
{
struct termp *p;
const char *toks[2];
@@ -92,8 +306,10 @@ term_alloc(char *outopts, enum termenc enc)
exit(EXIT_FAILURE);
}
+ p->type = type;
p->tabwidth = 5;
p->enc = enc;
+
width = 80;
while (outopts && *outopts)
@@ -228,11 +444,10 @@ term_flushln(struct termp *p)
*/
if (vend > bp && 0 == jhy && vis > 0) {
vend -= vis;
- putchar('\n');
+ endline(p);
if (TERMP_NOBREAK & p->flags) {
p->viscol = p->rmargin;
- for (j = 0; j < (int)p->rmargin; j++)
- putchar(' ');
+ advance(p, p->rmargin);
vend += p->rmargin - p->offset;
} else {
p->viscol = 0;
@@ -276,16 +491,15 @@ term_flushln(struct termp *p)
* so write preceding white space now.
*/
if (vbl) {
- for (j = 0; j < (int)vbl; j++)
- putchar(' ');
+ advance(p, vbl);
p->viscol += vbl;
vbl = 0;
}
if (ASCII_HYPH == p->buf[i])
- putchar('-');
+ letter(p, '-');
else
- putchar(p->buf[i]);
+ letter(p, p->buf[i]);
p->viscol += 1;
}
@@ -298,7 +512,7 @@ term_flushln(struct termp *p)
if ( ! (TERMP_NOBREAK & p->flags)) {
p->viscol = 0;
- putchar('\n');
+ endline(p);
return;
}
@@ -331,13 +545,12 @@ term_flushln(struct termp *p)
if (maxvis > vis + /* LINTED */
((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
p->viscol += maxvis - vis;
- for ( ; vis < maxvis; vis++)
- putchar(' ');
+ advance(p, maxvis - vis);
+ vis += (maxvis - vis);
} else { /* ...or newline break. */
- putchar('\n');
+ endline(p);
p->viscol = p->rmargin;
- for (i = 0; i < (int)p->rmargin; i++)
- putchar(' ');
+ advance(p, p->rmargin);
}
}
@@ -373,7 +586,7 @@ term_vspace(struct termp *p)
term_newln(p);
p->viscol = 0;
- putchar('\n');
+ endline(p);
}
@@ -625,7 +838,10 @@ encode(struct termp *p, const char *word, size_t sz)
* character by character.
*/
- if (TERMFONT_NONE == (f = term_fonttop(p))) {
+ if (TERMTYPE_PS == p->type) {
+ buffera(p, word, sz);
+ return;
+ } else if (TERMFONT_NONE == (f = term_fonttop(p))) {
buffera(p, word, sz);
return;
}
diff --git a/term.h b/term.h
index 0adbfd13..364b44ef 100644
--- a/term.h
+++ b/term.h
@@ -19,10 +19,17 @@
__BEGIN_DECLS
+struct termp;
+
enum termenc {
TERMENC_ASCII
};
+enum termtype {
+ TERMTYPE_CHAR,
+ TERMTYPE_PS
+};
+
enum termfont {
TERMFONT_NONE = 0,
TERMFONT_BOLD,
@@ -31,7 +38,10 @@ enum termfont {
#define TERM_MAXMARGIN 100000 /* FIXME */
+typedef void (*term_margin)(struct termp *, const void *);
+
struct termp {
+ enum termtype type;
size_t defrmargin; /* Right margin of the device.. */
size_t rmargin; /* Current right margin. */
size_t maxrmargin; /* Max right margin. */
@@ -60,12 +70,24 @@ struct termp {
enum termfont fontl; /* Last font set. */
enum termfont fontq[10]; /* Symmetric fonts. */
int fonti; /* Index of font stack. */
+ term_margin headf; /* invoked to print head */
+ term_margin footf; /* invoked to print foot */
+ const void *argf; /* arg for headf/footf */
+ int psstate; /* -Tps: state of ps output */
+#define PS_INLINE (1 << 0)
+#define PS_MARGINS (1 << 1)
+ size_t pscol; /* -Tps: visible column */
+ size_t psrow; /* -Tps: visible row */
+ size_t pspage; /* -Tps: current page */
};
void term_newln(struct termp *);
void term_vspace(struct termp *);
void term_word(struct termp *, const char *);
void term_flushln(struct termp *);
+void term_begin(struct termp *, term_margin,
+ term_margin, const void *);
+void term_end(struct termp *);
size_t term_hspan(const struct roffsu *);
size_t term_vspan(const struct roffsu *);