=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.11 retrieving revision 1.89 diff -u -p -r1.11 -r1.89 --- docbook2mdoc/docbook2mdoc.c 2014/03/29 11:13:49 1.11 +++ docbook2mdoc/docbook2mdoc.c 2019/04/03 16:52:51 1.89 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.11 2014/03/29 11:13:49 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.89 2019/04/03 16:52:51 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons + * Copyright (c) 2019 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,1071 +15,876 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - #include #include -#include -#include -#include #include #include -#include -#include -/* - * All recognised node types. - */ -enum nodeid { - NODE_ROOT = 0, /* Must comes first. */ - /* Alpha-ordered hereafter. */ - NODE_ARG, - NODE_CITEREFENTRY, - NODE_CMDSYNOPSIS, - NODE_CODE, - NODE_COMMAND, - NODE_FUNCDEF, - NODE_FUNCPROTOTYPE, - NODE_FUNCSYNOPSIS, - NODE_FUNCSYNOPSISINFO, - NODE_FUNCTION, - NODE_MANVOLNUM, - NODE_OPTION, - NODE_PARA, - NODE_PARAMDEF, - NODE_PARAMETER, - NODE_PROGRAMLISTING, - NODE_REFCLASS, - NODE_REFDESCRIPTOR, - NODE_REFENTRY, - NODE_REFENTRYTITLE, - NODE_REFMETA, - NODE_REFMISCINFO, - NODE_REFNAME, - NODE_REFNAMEDIV, - NODE_REFPURPOSE, - NODE_REFSECT1, - NODE_REFSYNOPSISDIV, - NODE_STRUCTNAME, - NODE_SYNOPSIS, - NODE_TEXT, - NODE_TITLE, - NODE__MAX -}; +#include "node.h" +#include "macro.h" +#include "format.h" /* - * Global parse state. - * Keep this as simple and small as possible. + * The implementation of the mdoc(7) formatter. */ -struct parse { - enum nodeid node; /* current (NODE_ROOT if pre-tree) */ - int stop; /* should we stop now? */ - struct pnode *root; /* root of parse tree */ - struct pnode *cur; /* current node in tree */ - char *b; /* nil-terminated buffer for pre-print */ - size_t bsz; /* current length of b */ - size_t mbsz; /* max bsz allocation */ - int newln; /* output: are we on a fresh line */ -}; -struct node { - const char *name; /* docbook element name */ - unsigned int flags; -#define NODE_IGNTEXT 1 /* ignore all contained text */ -}; +static void pnode_print(struct format *, struct pnode *); -TAILQ_HEAD(pnodeq, pnode); -struct pnode { - enum nodeid node; /* node type */ - char *b; /* binary data buffer */ - size_t bsz; /* data buffer size */ - struct pnode *parent; /* parent (or NULL if top) */ - struct pnodeq childq; /* queue of children */ - TAILQ_ENTRY(pnode) child; -}; +static void +pnode_printpara(struct format *p, struct pnode *pn) +{ + struct pnode *pp; -static const struct node nodes[NODE__MAX] = { - { NULL, 0 }, - { "arg", 0 }, - { "citerefentry", NODE_IGNTEXT }, - { "cmdsynopsis", NODE_IGNTEXT }, - { "code", 0 }, - { "command", 0 }, - { "funcdef", 0 }, - { "funcprototype", NODE_IGNTEXT }, - { "funcsynopsis", NODE_IGNTEXT }, - { "funcsynopsisinfo", 0 }, - { "function", 0 }, - { "manvolnum", 0 }, - { "option", 0 }, - { "para", 0 }, - { "paramdef", 0 }, - { "parameter", 0 }, - { "programlisting", 0 }, - { "refclass", NODE_IGNTEXT }, - { "refdescriptor", NODE_IGNTEXT }, - { "refentry", NODE_IGNTEXT }, - { "refentrytitle", 0 }, - { "refmeta", NODE_IGNTEXT }, - { "refmiscinfo", NODE_IGNTEXT }, - { "refname", 0 }, - { "refnamediv", NODE_IGNTEXT }, - { "refpurpose", 0 }, - { "refsect1", 0 }, - { "refsynopsisdiv", NODE_IGNTEXT }, - { "structname", 0 }, - { "synopsis", 0 }, - { NULL, 0 }, - { "title", 0 }, -}; + if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL && + (pp = pn->parent) == NULL) + return; + switch (pp->node) { + case NODE_ENTRY: + case NODE_LISTITEM: + return; + case NODE_PREFACE: + case NODE_SECTION: + if (p->level < 3) + return; + break; + default: + break; + } + macro_line(p, "Pp"); +} + +/* + * If the SYNOPSIS macro has a superfluous title, kill it. + */ static void -pnode_print(struct parse *p, struct pnode *pn); +pnode_printrefsynopsisdiv(struct format *p, struct pnode *pn) +{ + struct pnode *pp, *pq; + TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) + if (pp->node == NODE_TITLE) + pnode_unlink(pp); + + macro_line(p, "Sh SYNOPSIS"); +} + /* - * Look up whether "parent" is a valid parent for "node". - * This is sucked directly from the DocBook specification: look at the - * "children" and "parent" sections of each node. + * Start a hopefully-named `Sh' section. */ -static int -isparent(enum nodeid node, enum nodeid parent) +static void +pnode_printrefsect(struct format *p, struct pnode *pn) { + struct pnode *pp; + const char *title; + int flags, level; - switch (node) { - case (NODE_ROOT): - return(0); - case (NODE_ARG): - switch (parent) { - case (NODE_ARG): - case (NODE_CMDSYNOPSIS): - return(1); - default: + if (pn->parent == NULL) + return; + + level = ++p->level; + flags = ARG_SPACE; + if (level == 1) + flags |= ARG_UPPER; + if (level < 3) { + switch (pn->node) { + case NODE_CAUTION: + case NODE_NOTE: + case NODE_TIP: + case NODE_WARNING: + level = 3; break; - } - return(0); - case (NODE_CITEREFENTRY): - switch (parent) { - case (NODE_FUNCSYNOPSISINFO): - case (NODE_PARA): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); default: break; } - return(0); - case (NODE_CMDSYNOPSIS): - switch (parent) { - case (NODE_PARA): - case (NODE_REFSECT1): - case (NODE_REFSYNOPSISDIV): - return(1); - default: + } + + TAILQ_FOREACH(pp, &pn->childq, child) + if (pp->node == NODE_TITLE) break; - } - return(0); - case (NODE_CODE): - switch (parent) { - case (NODE_FUNCSYNOPSISINFO): - case (NODE_PARA): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); - default: + + if (pp == NULL) { + switch (pn->node) { + case NODE_PREFACE: + title = "Preface"; break; - } - return(0); - case (NODE_COMMAND): - switch (parent) { - case (NODE_CMDSYNOPSIS): - case (NODE_FUNCSYNOPSISINFO): - case (NODE_PARA): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); - default: + case NODE_CAUTION: + title = "Caution"; break; - } - return(0); - case (NODE_FUNCDEF): - return(NODE_FUNCPROTOTYPE == parent); - case (NODE_FUNCPROTOTYPE): - return(NODE_FUNCSYNOPSIS == parent); - case (NODE_FUNCSYNOPSIS): - switch (parent) { - case (NODE_PARA): - case (NODE_REFSECT1): - case (NODE_REFSYNOPSISDIV): - return(1); - default: + case NODE_NOTE: + title = "Note"; break; - } - return(0); - case (NODE_FUNCSYNOPSISINFO): - return(NODE_FUNCSYNOPSIS == parent); - case (NODE_FUNCTION): - switch (parent) { - case (NODE_CODE): - case (NODE_FUNCDEF): - case (NODE_FUNCSYNOPSISINFO): - case (NODE_PARA): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); - default: + case NODE_TIP: + title = "Tip"; break; - } - return(0); - case (NODE_MANVOLNUM): - switch (parent) { - case (NODE_CITEREFENTRY): - case (NODE_REFMETA): - return(1); - default: + case NODE_WARNING: + title = "Warning"; break; - } - return(0); - case (NODE_OPTION): - switch (parent) { - case (NODE_ARG): - case (NODE_FUNCSYNOPSISINFO): - case (NODE_PARA): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); default: + title = "Unknown"; break; } - return(0); - case (NODE_PARA): - switch (parent) { - case (NODE_REFSECT1): - case (NODE_REFSYNOPSISDIV): - return(1); - default: - break; - } - return(0); - case (NODE_PARAMDEF): - return(NODE_FUNCPROTOTYPE == parent); - case (NODE_PARAMETER): - switch (parent) { - case (NODE_CODE): - case (NODE_FUNCSYNOPSISINFO): - case (NODE_PARA): - case (NODE_PARAMDEF): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); - default: - break; - } - return(0); - case (NODE_PROGRAMLISTING): - switch (parent) { - case (NODE_PARA): - case (NODE_REFSECT1): - case (NODE_REFSYNOPSISDIV): - return(1); - default: - break; - } - return(0); - case (NODE_REFCLASS): - return(parent == NODE_REFNAMEDIV); - case (NODE_REFDESCRIPTOR): - return(parent == NODE_REFNAMEDIV); - case (NODE_REFENTRY): - return(parent == NODE_ROOT); - case (NODE_REFENTRYTITLE): - switch (parent) { - case (NODE_CITEREFENTRY): - case (NODE_REFMETA): - return(1); - default: - break; - } - case (NODE_REFMETA): - return(parent == NODE_REFENTRY); - case (NODE_REFMISCINFO): - return(parent == NODE_REFMETA); - case (NODE_REFNAME): - return(parent == NODE_REFNAMEDIV); - case (NODE_REFNAMEDIV): - return(parent == NODE_REFENTRY); - case (NODE_REFPURPOSE): - return(parent == NODE_REFNAMEDIV); - case (NODE_REFSECT1): - return(parent == NODE_REFENTRY); - case (NODE_REFSYNOPSISDIV): - return(parent == NODE_REFENTRY); - case (NODE_STRUCTNAME): - switch (parent) { - case (NODE_CODE): - case (NODE_FUNCSYNOPSISINFO): - case (NODE_FUNCTION): - case (NODE_OPTION): - case (NODE_PARA): - case (NODE_PARAMETER): - case (NODE_PROGRAMLISTING): - case (NODE_REFDESCRIPTOR): - case (NODE_REFENTRYTITLE): - case (NODE_REFNAME): - case (NODE_REFPURPOSE): - case (NODE_SYNOPSIS): - case (NODE_TITLE): - return(1); - default: - break; - } - return(0); - case (NODE_SYNOPSIS): - switch (parent) { - case (NODE_REFSYNOPSISDIV): - case (NODE_REFSECT1): - return(1); - default: - break; - } - return(0); - case (NODE_TITLE): - switch (parent) { - case (NODE_REFSECT1): - case (NODE_REFSYNOPSISDIV): - return(1); - default: - break; - } - return(0); - case (NODE_TEXT): - return(1); - case (NODE__MAX): + } + + switch (level) { + case 1: + macro_open(p, "Sh"); break; + case 2: + macro_open(p, "Ss"); + break; + default: + pnode_printpara(p, pn); + macro_open(p, "Sy"); + break; } - abort(); - return(0); + if (pp != NULL) { + macro_addnode(p, pp, flags); + pnode_unlink(pp); + } else + macro_addarg(p, title, ARG_SPACE | ARG_QUOTED); + macro_close(p); } /* - * Process a stream of characters. - * We store text as nodes in and of themselves. - * If a text node is already open, append to it. - * If it's not open, open one under the current context. + * Start a reference, extracting the title and volume. */ static void -xml_char(void *arg, const XML_Char *p, int sz) +pnode_printciterefentry(struct format *p, struct pnode *pn) { - struct parse *ps = arg; - struct pnode *dat; - int i; + struct pnode *pp, *title, *manvol; - /* Stopped or no tree yet. */ - if (ps->stop || NODE_ROOT == ps->node) - return; - - /* Not supposed to be collecting text. */ - assert(NULL != ps->cur); - if (NODE_IGNTEXT & nodes[ps->node].flags) - return; - - /* - * Are we in the midst of processing text? - * If we're not processing text right now, then create a text - * node for doing so. - * However, don't do so unless we have some non-whitespace to - * process: strip out all leading whitespace to be sure. - */ - if (NODE_TEXT != ps->node) { - for (i = 0; i < sz; i++) - if ( ! isspace((int)p[i])) - break; - if (i == sz) - return; - p += i; - sz -= i; - dat = calloc(1, sizeof(struct pnode)); - if (NULL == dat) { - perror(NULL); - exit(EXIT_FAILURE); - } - - dat->node = ps->node = NODE_TEXT; - dat->parent = ps->cur; - TAILQ_INIT(&dat->childq); - TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); - ps->cur = dat; - assert(NULL != ps->root); + title = manvol = NULL; + TAILQ_FOREACH(pp, &pn->childq, child) { + if (pp->node == NODE_MANVOLNUM) + manvol = pp; + else if (pp->node == NODE_REFENTRYTITLE) + title = pp; } - - /* Append to current buffer. */ - assert(sz >= 0); - ps->cur->b = realloc(ps->cur->b, - ps->cur->bsz + (size_t)sz); - if (NULL == ps->cur->b) { - perror(NULL); - exit(EXIT_FAILURE); - } - memcpy(ps->cur->b + ps->cur->bsz, p, sz); - ps->cur->bsz += (size_t)sz; + macro_open(p, "Xr"); + if (title == NULL) + macro_addarg(p, "unknown", ARG_SPACE); + else + macro_addnode(p, title, ARG_SPACE | ARG_SINGLE); + if (manvol == NULL) + macro_addarg(p, "1", ARG_SPACE); + else + macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); + pnode_unlinksub(pn); } static void -pnode_trim(struct pnode *pn) +pnode_printrefmeta(struct format *p, struct pnode *pn) { + struct pnode *pp, *title, *manvol; - assert(NODE_TEXT == pn->node); - for ( ; pn->bsz > 0; pn->bsz--) - if ( ! isspace((int)pn->b[pn->bsz - 1])) - break; + title = manvol = NULL; + TAILQ_FOREACH(pp, &pn->childq, child) { + if (pp->node == NODE_MANVOLNUM) + manvol = pp; + else if (pp->node == NODE_REFENTRYTITLE) + title = pp; + } + macro_open(p, "Dt"); + if (title == NULL) + macro_addarg(p, "UNKNOWN", ARG_SPACE); + else + macro_addnode(p, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER); + if (manvol == NULL) + macro_addarg(p, "1", ARG_SPACE); + else + macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); + macro_close(p); + pnode_unlink(pn); } -/* - * Begin an element. - * First, look for the element. - * If we don't find it and we're not parsing, keep going. - * If we don't find it and we're parsing, puke and exit. - * If we find it but we're not parsing yet (i.e., it's not a refentry - * and thus out of context), keep going. - * If we find it and we're at the root and already have a tree, puke and - * exit (FIXME: I don't think this is right?). - * If we find it but we're parsing a text node, close out the text node, - * return to its parent, and keep going. - * Make sure that the element is in the right context. - * Lastly, put the node onto our parse tree and continue. - */ static void -xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts) +pnode_printfuncdef(struct format *f, struct pnode *n) { - struct parse *ps = arg; - enum nodeid node; - struct pnode *dat; + struct pnode *nc; - if (ps->stop) - return; - - /* Close out text node, if applicable... */ - if (NODE_TEXT == ps->node) { - assert(NULL != ps->cur); - pnode_trim(ps->cur); - ps->cur = ps->cur->parent; - assert(NULL != ps->cur); - ps->node = ps->cur->node; + nc = TAILQ_FIRST(&n->childq); + if (nc != NULL && nc->node == NODE_TEXT) { + macro_argline(f, "Ft", nc->b); + pnode_unlink(nc); } - - for (node = 0; node < NODE__MAX; node++) - if (NULL == nodes[node].name) - continue; - else if (0 == strcmp(nodes[node].name, name)) - break; - - /* FIXME: do more with these error messages... */ - if (NODE__MAX == node && NODE_ROOT == ps->node) { - fprintf(stderr, "%s: ignoring node\n", name); - return; - } else if (NODE__MAX == node) { - fprintf(stderr, "%s: unknown node\n", name); - ps->stop = 1; - return; - } else if (NODE_ROOT == ps->node && NULL != ps->root) { - fprintf(stderr, "%s: reentering?\n", name); - ps->stop = 1; - return; - } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) { - fprintf(stderr, "%s: known node w/o context\n", name); - return; - } else if ( ! isparent(node, ps->node)) { - fprintf(stderr, "%s: bad parent\n", name); - ps->stop = 1; - return; - } - - if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { - perror(NULL); - exit(EXIT_FAILURE); - } - - dat->node = ps->node = node; - dat->parent = ps->cur; - TAILQ_INIT(&dat->childq); - - if (NULL != ps->cur) - TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); - - ps->cur = dat; - if (NULL == ps->root) - ps->root = dat; + macro_nodeline(f, "Fo", n, ARG_SINGLE); + pnode_unlinksub(n); } /* - * Roll up the parse tree. - * If we're at a text node, roll that one up first. - * If we hit the root, then assign ourselves as the NODE_ROOT. + * The node is a little peculiar. + * First, it can have arbitrary open and closing tokens, which default + * to parentheses. + * Second, >1 arguments are separated by commas. */ static void -xml_elem_end(void *arg, const XML_Char *name) +pnode_printmathfenced(struct format *p, struct pnode *pn) { - struct parse *ps = arg; + struct pnode *pp; - if (ps->stop || NODE_ROOT == ps->node) - return; + printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "(")); - /* Close out text node, if applicable... */ - if (NODE_TEXT == ps->node) { - assert(NULL != ps->cur); - pnode_trim(ps->cur); - ps->cur = ps->cur->parent; - assert(NULL != ps->cur); - ps->node = ps->cur->node; - } + pp = TAILQ_FIRST(&pn->childq); + pnode_print(p, pp); - if (NULL == (ps->cur = ps->cur->parent)) - ps->node = NODE_ROOT; - else - ps->node = ps->cur->node; + while ((pp = TAILQ_NEXT(pp, child)) != NULL) { + putchar(','); + pnode_print(p, pp); + } + printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")")); + pnode_unlinksub(pn); } /* - * Recursively free a node (NULL is ok). + * These math nodes require special handling because they have infix + * syntax, instead of the usual prefix or prefix. + * So we need to break up the first and second child node with a + * particular eqn(7) word. */ static void -pnode_free(struct pnode *pn) +pnode_printmath(struct format *p, struct pnode *pn) { struct pnode *pp; - if (NULL == pn) - return; + pp = TAILQ_FIRST(&pn->childq); + pnode_print(p, pp); - while (NULL != (pp = TAILQ_FIRST(&pn->childq))) { - TAILQ_REMOVE(&pn->childq, pp, child); - pnode_free(pp); + switch (pn->node) { + case NODE_MML_MSUP: + fputs(" sup ", stdout); + break; + case NODE_MML_MFRAC: + fputs(" over ", stdout); + break; + case NODE_MML_MSUB: + fputs(" sub ", stdout); + break; + default: + break; } - free(pn->b); - free(pn); + pp = TAILQ_NEXT(pp, child); + pnode_print(p, pp); + pnode_unlinksub(pn); } -/* - * Unlink a node from its parent and pnode_free() it. - */ static void -pnode_unlink(struct pnode *pn) +pnode_printfuncprototype(struct format *p, struct pnode *pn) { + struct pnode *pp, *fdef; - if (NULL != pn->parent) - TAILQ_REMOVE(&pn->parent->childq, pn, child); - pnode_free(pn); -} + TAILQ_FOREACH(fdef, &pn->childq, child) + if (fdef->node == NODE_FUNCDEF) + break; -/* - * Unlink all children of a node and pnode_free() them. - */ -static void -pnode_unlinksub(struct pnode *pn) -{ + if (fdef != NULL) { + pnode_printfuncdef(p, fdef); + pnode_unlink(fdef); + } else + macro_line(p, "Fo UNKNOWN"); - while ( ! TAILQ_EMPTY(&pn->childq)) - pnode_unlink(TAILQ_FIRST(&pn->childq)); + TAILQ_FOREACH(pp, &pn->childq, child) + macro_nodeline(p, "Fa", pp, ARG_SINGLE); + + macro_line(p, "Fc"); + pnode_unlinksub(pn); } /* - * Reset the lookaside buffer. + * The element is more complicated than it should be because text + * nodes are treated like ".Ar foo", but non-text nodes need to be + * re-sent into the printer (i.e., without the preceding ".Ar"). + * This also handles the case of "repetition" (or in other words, the + * ellipsis following an argument) and optionality. */ static void -bufclear(struct parse *p) +pnode_printarg(struct format *p, struct pnode *pn) { + struct pnode *pp; + struct pattr *ap; + int isop, isrep; - p->b[p->bsz = 0] = '\0'; + isop = 1; + isrep = 0; + TAILQ_FOREACH(ap, &pn->attrq, child) { + if (ap->key == ATTRKEY_CHOICE && + (ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ)) + isop = 0; + else if (ap->key == ATTRKEY_REP && ap->val == ATTRVAL_REPEAT) + isrep = 1; + } + if (isop) + macro_open(p, "Op"); + + TAILQ_FOREACH(pp, &pn->childq, child) { + if (pp->node == NODE_TEXT) + macro_open(p, "Ar"); + pnode_print(p, pp); + if (isrep && pp->node == NODE_TEXT) + macro_addarg(p, "...", ARG_SPACE); + } + pnode_unlinksub(pn); } -/* - * Append NODE_TEXT contents to the current buffer, reallocating its - * size if necessary. - * The buffer is ALWAYS nil-terminated. - */ static void -bufappend(struct parse *p, struct pnode *pn) +pnode_printgroup(struct format *p, struct pnode *pn) { + struct pnode *pp, *np; + struct pattr *ap; + int isop, sv; - assert(NODE_TEXT == pn->node); - if (p->bsz + pn->bsz + 1 > p->mbsz) { - p->mbsz = p->bsz + pn->bsz + 1; - if (NULL == (p->b = realloc(p->b, p->mbsz))) { - perror(NULL); - exit(EXIT_FAILURE); + isop = 1; + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ap->key == ATTRKEY_CHOICE && + (ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ)) { + isop = 0; + break; } + + /* + * Make sure we're on a macro line. + * This will prevent pnode_print() for putting us on a + * subsequent line. + */ + sv = p->linestate == LINE_NEW; + if (isop) + macro_open(p, "Op"); + else if (sv) + macro_open(p, "No"); + + /* + * Keep on printing text separated by the vertical bar as long + * as we're within the same origin node as the group. + * This is kind of a nightmare. + * Eh, DocBook... + * FIXME: if there's a "Fl", we don't cut off the leading "-" + * like we do in pnode_print(). + */ + TAILQ_FOREACH(pp, &pn->childq, child) { + pnode_print(p, pp); + np = TAILQ_NEXT(pp, child); + while (np != NULL) { + if (pp->node != np->node) + break; + macro_addarg(p, "|", ARG_SPACE); + macro_addnode(p, np, ARG_SPACE); + pp = np; + np = TAILQ_NEXT(np, child); + } } - memcpy(p->b + p->bsz, pn->b, pn->bsz); - p->bsz += pn->bsz; - p->b[p->bsz] = '\0'; + if (sv) + macro_close(p); + pnode_unlinksub(pn); } -/* - * Recursively append all NODE_TEXT nodes to the buffer. - * This descends into non-text nodes, but doesn't do anything beyond - * them. - * In other words, this is a recursive text grok. - */ static void -bufappend_r(struct parse *p, struct pnode *pn) +pnode_printauthor(struct format *f, struct pnode *n) { - struct pnode *pp; + struct pnode *nc, *ncn; + int have_contrib, have_name; - if (NODE_TEXT == pn->node) - bufappend(p, pn); - TAILQ_FOREACH(pp, &pn->childq, child) - bufappend_r(p, pp); -} + /* + * Print children up front, before the .An scope, + * and figure out whether we a name of a person. + */ -/* - * Recursively print text presumably on a macro line. - * Convert all whitespace to regular spaces. - */ -static void -pnode_printmacrolinepart(struct parse *p, struct pnode *pn) -{ - char *cp; + have_contrib = have_name = 0; + TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) { + switch (nc->node) { + case NODE_CONTRIB: + if (have_contrib) + print_text(f, ",", 0); + print_textnode(f, nc); + pnode_unlink(nc); + have_contrib = 1; + break; + case NODE_PERSONNAME: + have_name = 1; + break; + default: + break; + } + } + if (TAILQ_FIRST(&n->childq) == NULL) + return; - bufclear(p); - bufappend_r(p, pn); + if (have_contrib) + print_text(f, ":", 0); - /* Convert all space to spaces. */ - for (cp = p->b; '\0' != *cp; cp++) - if (isspace((int)*cp)) - *cp = ' '; + /* + * If we have a name, print it in the .An scope and leave + * all other content for child handlers, to print after the + * scope. Otherwise, print everything in the scope. + */ - for (cp = p->b; isspace((int)*cp); cp++) - /* Spin past whitespace (XXX: necessary?) */ ; - for ( ; '\0' != *cp; cp++) { - /* Escape us if we look like a macro. */ - if ((cp == p->b || ' ' == *(cp - 1)) && - isupper((int)*cp) && - '\0' != *(cp + 1) && - islower((int)*(cp + 1)) && - ('\0' == *(cp + 2) || - ' ' == *(cp + 2) || - (islower((int)*(cp + 2)) && - ('\0' == *(cp + 3) || - ' ' == *(cp + 3))))) - fputs("\\&", stdout); - putchar(*cp); - /* If we're a character escape, escape us. */ - if ('\\' == *cp) - putchar('e'); + macro_open(f, "An"); + TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) { + if (nc->node == NODE_PERSONNAME || have_name == 0) { + macro_addnode(f, nc, ARG_SPACE); + pnode_unlink(nc); + } } -} -/* - * Just pnode_printmacrolinepart() but with a newline. - * If no text, just the newline. - */ -static void -pnode_printmacroline(struct parse *p, struct pnode *pn) -{ + /* + * If there is an email address, + * print it on the same macro line. + */ - pnode_printmacrolinepart(p, pn); - putchar('\n'); -} + if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) { + pnode_print(f, nc); + pnode_unlink(nc); + } -static void -pnode_printmopen(struct parse *p) -{ - if (p->newln) { - putchar('.'); - p->newln = 0; - } else - putchar(' '); -} + /* + * If there are still unprinted children, end the scope + * with a comma. Otherwise, leave the scope open in case + * a text node follows that starts with closing punctuation. + */ -static void -pnode_printmclose(struct parse *p, int sv) -{ - - if (sv && ! p->newln) { - putchar('\n'); - p->newln = 1; + if (TAILQ_FIRST(&n->childq) != NULL) { + macro_addarg(f, ",", ARG_SPACE); + macro_close(f); } } -/* - * If the SYNOPSIS macro has a superfluous title, kill it. - */ static void -pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn) +pnode_printprologue(struct format *p, struct ptree *tree) { - struct pnode *pp; + struct pnode *refmeta; - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_TITLE == pp->node) { - pnode_unlink(pp); - return; - } + refmeta = tree->root == NULL ? NULL : + pnode_findfirst(tree->root, NODE_REFMETA); + + macro_line(p, "Dd $Mdocdate" "$"); + if (refmeta == NULL) { + macro_open(p, "Dt"); + macro_addarg(p, + pnode_getattr_raw(tree->root, ATTRKEY_ID, "UNKNOWN"), + ARG_SPACE | ARG_SINGLE | ARG_UPPER); + macro_addarg(p, "1", ARG_SPACE); + macro_close(p); + } else + pnode_printrefmeta(p, refmeta); + macro_line(p, "Os"); + + if (tree->flags & TREE_EQN) { + macro_line(p, "EQ"); + print_text(p, "delim $$", 0); + macro_line(p, "EN"); + } } /* - * Start a hopefully-named `Sh' section. + * We can have multiple elements within a , which + * we should comma-separate as list headers. */ static void -pnode_printrefsect(struct parse *p, struct pnode *pn) +pnode_printvarlistentry(struct format *p, struct pnode *pn) { struct pnode *pp; + int first = 1; + macro_open(p, "It"); + TAILQ_FOREACH(pp, &pn->childq, child) { + if (pp->node != NODE_TERM) + continue; + if ( ! first) + macro_addarg(p, ",", 0); + pnode_print(p, pp); + first = 0; + } + macro_close(p); TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_TITLE == pp->node) - break; - - fputs(".Sh ", stdout); - - if (NULL != pp) { - pnode_printmacroline(p, pp); - pnode_unlink(pp); - } else - puts("UNKNOWN"); + if (pp->node != NODE_TERM) + pnode_print(p, pp); + pnode_unlinksub(pn); } -/* - * Start a reference, extracting the title and volume. - */ static void -pnode_printciterefentry(struct parse *p, struct pnode *pn) +pnode_printtitle(struct format *p, struct pnode *pn) { - struct pnode *pp, *title, *manvol; + struct pnode *pp, *pq; - title = manvol = NULL; - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_MANVOLNUM == pp->node) - manvol = pp; - else if (NODE_REFENTRYTITLE == pp->node) - title = pp; - - fputs(".Xr ", stdout); - - if (NULL != title) { - pnode_printmacrolinepart(p, title); - putchar(' '); - } else - fputs("unknown ", stdout); - - if (NULL != manvol) - pnode_printmacroline(p, manvol); - else - puts("1"); + TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) { + if (pp->node == NODE_TITLE) { + pnode_printpara(p, pp); + pnode_print(p, pp); + pnode_unlink(pp); + } + } } static void -pnode_printrefmeta(struct parse *p, struct pnode *pn) +pnode_printrow(struct format *p, struct pnode *pn) { - struct pnode *pp, *title, *manvol; + struct pnode *pp; - title = manvol = NULL; - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_MANVOLNUM == pp->node) - manvol = pp; - else if (NODE_REFENTRYTITLE == pp->node) - title = pp; - - puts(".Dd $Mdocdate" "$"); - fputs(".Dt ", stdout); - - if (NULL != title) { - /* FIXME: uppercase. */ - pnode_printmacrolinepart(p, title); - putchar(' '); - } else - fputs("UNKNOWN ", stdout); - - if (NULL != manvol) - pnode_printmacroline(p, manvol); - else - puts("1"); - - puts(".Os"); + macro_line(p, "Bl -dash -compact"); + TAILQ_FOREACH(pp, &pn->childq, child) { + macro_line(p, "It"); + pnode_print(p, pp); + } + macro_line(p, "El"); + pnode_unlink(pn); } static void -pnode_printfuncdef(struct parse *p, struct pnode *pn) +pnode_printtgroup1(struct format *f, struct pnode *n) { - struct pnode *pp, *ftype, *func; + struct pnode *nc; - ftype = func = NULL; - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_TEXT == pp->node) - ftype = pp; - else if (NODE_FUNCTION == pp->node) - func = pp; - - if (NULL != ftype) { - fputs(".Ft ", stdout); - pnode_printmacroline(p, ftype); + macro_line(f, "Bl -bullet -compact"); + while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) { + macro_line(f, "It"); + pnode_print(f, nc); + pnode_unlink(nc); } - - if (NULL != func) { - fputs(".Fo ", stdout); - pnode_printmacroline(p, func); - } else - puts(".Fo UNKNOWN"); + macro_line(f, "El"); + pnode_unlinksub(n); } static void -pnode_printparamdef(struct parse *p, struct pnode *pn) +pnode_printtgroup2(struct format *f, struct pnode *n) { - struct pnode *pp, *ptype, *param; + struct pnode *nr, *ne; - ptype = param = NULL; - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_TEXT == pp->node) - ptype = pp; - else if (NODE_PARAMETER == pp->node) - param = pp; - - fputs(".Fa \"", stdout); - if (NULL != ptype) { - pnode_printmacrolinepart(p, ptype); - putchar(' '); + macro_line(f, "Bl -tag -width Ds"); + while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) { + if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL) + break; + macro_open(f, "It"); + pnode_print(f, ne); + macro_close(f); + pnode_unlink(ne); + pnode_print(f, nr); + pnode_unlink(nr); } - - if (NULL != param) - pnode_printmacrolinepart(p, param); - - puts("\""); + macro_line(f, "El"); + pnode_unlinksub(n); } static void -pnode_printfuncprototype(struct parse *p, struct pnode *pn) +pnode_printtgroup(struct format *f, struct pnode *n) { - struct pnode *pp, *fdef; + struct pnode *nc; - TAILQ_FOREACH(fdef, &pn->childq, child) - if (NODE_FUNCDEF == fdef->node) - break; + switch (atoi(pnode_getattr_raw(n, ATTRKEY_COLS, "0"))) { + case 1: + pnode_printtgroup1(f, n); + return; + case 2: + pnode_printtgroup2(f, n); + return; + default: + break; + } - if (NULL != fdef) - pnode_printfuncdef(p, fdef); - else - puts(".Fo UNKNOWN"); - - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_PARAMDEF == pp->node) - pnode_printparamdef(p, pp); - - puts(".Fc"); + macro_line(f, "Bl -ohang"); + while ((nc = pnode_findfirst(n, NODE_ROW)) != NULL) { + macro_line(f, "It Table Row"); + pnode_printrow(f, nc); + } + macro_line(f, "El"); + pnode_unlinksub(n); } -/* - * The element is more complicated than it should be because text - * nodes are treated like ".Ar foo", but non-text nodes need to be - * re-sent into the printer (i.e., without the preceding ".Ar"). - * TODO: handle "optional" attribute. - */ static void -pnode_printarg(struct parse *p, struct pnode *pn) +pnode_printlist(struct format *p, struct pnode *pn) { struct pnode *pp; + pnode_printtitle(p, pn); + macro_argline(p, "Bl", + pn->node == NODE_ORDEREDLIST ? "-enum" : "-bullet"); TAILQ_FOREACH(pp, &pn->childq, child) { - if (NODE_TEXT == pp->node) { - pnode_printmopen(p); - fputs("Ar ", stdout); - } + macro_line(p, "It"); pnode_print(p, pp); } + macro_line(p, "El"); + pnode_unlinksub(pn); } -/* - * Recursively search and return the first instance of "node". - */ -static struct pnode * -pnode_findfirst(struct pnode *pn, enum nodeid node) -{ - struct pnode *pp, *res; - - res = NULL; - TAILQ_FOREACH(pp, &pn->childq, child) { - res = pp->node == node ? pp : - pnode_findfirst(pp, node); - if (NULL != res) - break; - } - - return(res); -} - static void -pnode_printprologue(struct parse *p, struct pnode *pn) +pnode_printvariablelist(struct format *p, struct pnode *pn) { struct pnode *pp; - pp = NULL == p->root ? NULL : - pnode_findfirst(p->root, NODE_REFMETA); - - if (NULL != pp) { - pnode_printrefmeta(p, pp); - pnode_unlink(pp); - } else { - puts(".\\\" Supplying bogus prologue..."); - puts(".Dd $Mdocdate" "$"); - puts(".Dt UNKNOWN 1"); - puts(".Os"); + pnode_printtitle(p, pn); + macro_line(p, "Bl -tag -width Ds"); + TAILQ_FOREACH(pp, &pn->childq, child) { + if (pp->node == NODE_VARLISTENTRY) + pnode_printvarlistentry(p, pp); + else + macro_nodeline(p, "It", pp, 0); } + macro_line(p, "El"); + pnode_unlinksub(pn); } /* * Print a parsed node (or ignore it--whatever). * This is a recursive function. - * FIXME: macro line continuation? + * FIXME: if we're in a literal context ( or or + * whatever), don't print inline macros. */ static void -pnode_print(struct parse *p, struct pnode *pn) +pnode_print(struct format *p, struct pnode *pn) { struct pnode *pp; + const char *ccp; char *cp; - int last, sv; + int last; + enum linestate sv; - if (NULL == pn) + if (pn == NULL) return; - sv = p->newln; + sv = p->linestate; switch (pn->node) { - case (NODE_ARG): + case NODE_APPLICATION: + macro_open(p, "Nm"); + break; + case NODE_ARG: pnode_printarg(p, pn); - pnode_unlinksub(pn); break; - case (NODE_CITEREFENTRY): - assert(p->newln); + case NODE_AUTHOR: + pnode_printauthor(p, pn); + break; + case NODE_AUTHORGROUP: + macro_line(p, "An -split"); + break; + case NODE_BOOKINFO: + macro_line(p, "Sh NAME"); + break; + case NODE_CITEREFENTRY: pnode_printciterefentry(p, pn); - pnode_unlinksub(pn); break; - case (NODE_CODE): - pnode_printmopen(p); - fputs("Li ", stdout); + case NODE_CITETITLE: + macro_open(p, "%T"); break; - case (NODE_COMMAND): - pnode_printmopen(p); - fputs("Nm ", stdout); + case NODE_CODE: + macro_open(p, "Li"); break; - case (NODE_FUNCTION): - pnode_printmopen(p); - fputs("Fn ", stdout); + case NODE_COMMAND: + macro_open(p, "Nm"); break; - case (NODE_FUNCPROTOTYPE): - assert(p->newln); + case NODE_CONSTANT: + macro_open(p, "Dv"); + break; + case NODE_EDITOR: + print_text(p, "editor:", ARG_SPACE); + macro_open(p, "An"); + break; + case NODE_EMAIL: + macro_open(p, "Aq Mt"); + break; + case NODE_EMPHASIS: + case NODE_FIRSTTERM: + macro_open(p, "Em"); + break; + case NODE_ENVAR: + macro_open(p, "Ev"); + break; + case NODE_ESCAPE: + if (p->linestate == LINE_NEW) + p->linestate = LINE_TEXT; + else + putchar(' '); + fputs(pn->b, stdout); + break; + case NODE_FILENAME: + macro_open(p, "Pa"); + break; + case NODE_FUNCTION: + macro_open(p, "Fn"); + break; + case NODE_FUNCPROTOTYPE: pnode_printfuncprototype(p, pn); - pnode_unlinksub(pn); break; - case (NODE_FUNCSYNOPSISINFO): - pnode_printmopen(p); - fputs("Fd ", stdout); + case NODE_FUNCSYNOPSISINFO: + macro_open(p, "Fd"); break; - case (NODE_OPTION): - pnode_printmopen(p); - fputs("Fl ", stdout); + case NODE_INFORMALEQUATION: + macro_line(p, "EQ"); break; - case (NODE_PARA): - assert(p->newln); - puts(".Pp"); + case NODE_INLINEEQUATION: + if (p->linestate == LINE_NEW) + p->linestate = LINE_TEXT; + putchar('$'); break; - case (NODE_PARAMETER): - /* Suppress non-text children... */ - pnode_printmopen(p); - fputs("Fa \"", stdout); - pnode_printmacrolinepart(p, pn); - puts("\""); + case NODE_ITEMIZEDLIST: + pnode_printlist(p, pn); + break; + case NODE_GROUP: + pnode_printgroup(p, pn); + break; + case NODE_KEYSYM: + macro_open(p, "Sy"); + break; + case NODE_LEGALNOTICE: + macro_line(p, "Sh LEGAL NOTICE"); + break; + case NODE_LINK: + ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL); + if (ccp == NULL) + break; + macro_argline(p, "Sx", ccp); + return; + case NODE_LITERAL: + macro_open(p, "Li"); + break; + case NODE_LITERALLAYOUT: + macro_argline(p, "Bd", pnode_getattr(pn, ATTRKEY_CLASS) == + ATTRVAL_MONOSPACED ? "-literal" : "-unfilled"); + break; + case NODE_MML_MFENCED: + pnode_printmathfenced(p, pn); + break; + case NODE_MML_MROW: + case NODE_MML_MI: + case NODE_MML_MN: + case NODE_MML_MO: + if (TAILQ_EMPTY(&pn->childq)) + break; + fputs(" { ", stdout); + break; + case NODE_MML_MFRAC: + case NODE_MML_MSUB: + case NODE_MML_MSUP: + pnode_printmath(p, pn); + break; + case NODE_OPTION: + macro_open(p, "Fl"); + break; + case NODE_ORDEREDLIST: + pnode_printlist(p, pn); + break; + case NODE_PARA: + pnode_printpara(p, pn); + break; + case NODE_PARAMDEF: + case NODE_PARAMETER: + macro_nodeline(p, "Fa", pn, ARG_SINGLE); pnode_unlinksub(pn); break; - case (NODE_PROGRAMLISTING): - assert(p->newln); - puts(".Bd -literal"); + case NODE_QUOTE: + macro_open(p, "Qo"); break; - case (NODE_REFMETA): - abort(); + case NODE_PROGRAMLISTING: + case NODE_SCREEN: + case NODE_SYNOPSIS: + macro_line(p, "Bd -literal"); break; - case (NODE_REFNAME): + case NODE_REFENTRYINFO: + /* Suppress. */ + pnode_unlinksub(pn); + break; + case NODE_REFNAME: /* Suppress non-text children... */ - pnode_printmopen(p); - fputs("Nm ", stdout); - pnode_printmacrolinepart(p, pn); + macro_open(p, "Nm"); + macro_addnode(p, pn, ARG_SPACE | ARG_SINGLE); pnode_unlinksub(pn); break; - case (NODE_REFNAMEDIV): - assert(p->newln); - puts(".Sh NAME"); + case NODE_REFNAMEDIV: + macro_line(p, "Sh NAME"); break; - case (NODE_REFPURPOSE): - assert(p->newln); - fputs(".Nd ", stdout); + case NODE_REFPURPOSE: + macro_open(p, "Nd"); break; - case (NODE_REFSYNOPSISDIV): - assert(p->newln); + case NODE_REFSYNOPSISDIV: pnode_printrefsynopsisdiv(p, pn); - puts(".Sh SYNOPSIS"); break; - case (NODE_REFSECT1): - assert(p->newln); + case NODE_PREFACE: + case NODE_SECTION: + case NODE_NOTE: + case NODE_TIP: + case NODE_CAUTION: + case NODE_WARNING: pnode_printrefsect(p, pn); break; - case (NODE_STRUCTNAME): - pnode_printmopen(p); - fputs("Vt ", stdout); + case NODE_REPLACEABLE: + macro_open(p, "Ar"); break; - case (NODE_TEXT): - bufclear(p); - bufappend(p, pn); + case NODE_SBR: + macro_line(p, "br"); + break; + case NODE_SGMLTAG: + macro_open(p, "Li"); + break; + case NODE_STRUCTNAME: + macro_open(p, "Vt"); + break; + case NODE_TEXT: + if (pn->bsz == 0) { + assert(pn->real != pn->b); + break; + } + if (p->linestate == LINE_NEW) + p->linestate = LINE_TEXT; + else + putchar(' '); + /* * Output all characters, squeezing out whitespace - * between newlines. + * between newlines. * XXX: all whitespace, including tabs (?). * Remember to escape control characters and escapes. */ - assert(p->bsz); - for (last = '\n', cp = p->b; '\0' != *cp; ) { - if ('\n' == last) { + cp = pn->b; + + /* + * There's often a superfluous "-" in its