=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.7 retrieving revision 1.23 diff -u -p -r1.7 -r1.23 --- docbook2mdoc/docbook2mdoc.c 2014/03/28 10:37:50 1.7 +++ docbook2mdoc/docbook2mdoc.c 2014/03/30 17:46:17 1.23 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.7 2014/03/28 10:37:50 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.23 2014/03/30 17:46:17 kristaps Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * @@ -26,343 +26,130 @@ #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_SYNOPSIS, - NODE_TEXT, - NODE_TITLE, - NODE__MAX -}; +#include "extern.h" /* * Global parse state. * Keep this as simple and small as possible. */ struct parse { + XML_Parser xml; enum nodeid node; /* current (NODE_ROOT if pre-tree) */ + const char *fname; /* filename */ int stop; /* should we stop now? */ struct pnode *root; /* root of parse tree */ struct pnode *cur; /* current node in tree */ - char *b; - size_t bsz; - size_t mbsz; + 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; + const char *name; /* docbook element name */ unsigned int flags; #define NODE_IGNTEXT 1 /* ignore all contained text */ }; TAILQ_HEAD(pnodeq, pnode); +TAILQ_HEAD(pattrq, pattr); +struct pattr { + enum attrkey key; + enum attrval val; + char *rawval; + TAILQ_ENTRY(pattr) child; +}; + 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 */ + struct pattrq attrq; /* attributes of node */ TAILQ_ENTRY(pnode) child; }; +static const char *attrkeys[ATTRKEY__MAX] = { + "choice", + "id", + "rep" +}; + +static const char *attrvals[ATTRVAL__MAX] = { + "norepeat", + "opt", + "plain", + "repeat", + "req" +}; + static const struct node nodes[NODE__MAX] = { { NULL, 0 }, + { "acronym", 0 }, { "arg", 0 }, { "citerefentry", NODE_IGNTEXT }, { "cmdsynopsis", NODE_IGNTEXT }, { "code", 0 }, { "command", 0 }, + { "date", 0 }, + { "emphasis", 0 }, + { "envar", 0 }, + { "filename", 0 }, { "funcdef", 0 }, { "funcprototype", NODE_IGNTEXT }, { "funcsynopsis", NODE_IGNTEXT }, { "funcsynopsisinfo", 0 }, { "function", 0 }, + { "itemizedlist", NODE_IGNTEXT }, + { "link", 0 }, + { "listitem", NODE_IGNTEXT }, + { "literal", 0 }, { "manvolnum", 0 }, { "option", 0 }, + { "orderedlist", NODE_IGNTEXT }, { "para", 0 }, { "paramdef", 0 }, { "parameter", 0 }, { "programlisting", 0 }, + { "prompt", 0 }, { "refclass", NODE_IGNTEXT }, { "refdescriptor", NODE_IGNTEXT }, { "refentry", NODE_IGNTEXT }, + { "refentryinfo", NODE_IGNTEXT }, { "refentrytitle", 0 }, { "refmeta", NODE_IGNTEXT }, { "refmiscinfo", NODE_IGNTEXT }, { "refname", 0 }, { "refnamediv", NODE_IGNTEXT }, { "refpurpose", 0 }, - { "refsect1", 0 }, + { "refsect1", NODE_IGNTEXT }, + { "refsect2", NODE_IGNTEXT }, { "refsynopsisdiv", NODE_IGNTEXT }, + { "replaceable", 0 }, + { "sbr", NODE_IGNTEXT }, + { "screen", NODE_IGNTEXT }, + { "structname", 0 }, { "synopsis", 0 }, + { "term", 0 }, { NULL, 0 }, { "title", 0 }, + { "ulink", 0 }, + { "userinput", 0 }, + { "variablelist", NODE_IGNTEXT }, + { "varlistentry", NODE_IGNTEXT }, }; +static void +pnode_print(struct parse *p, struct pnode *pn); + /* - * Look up whether "parent" is a valid parent for "node". + * 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. */ -static int -isparent(enum nodeid node, enum nodeid parent) -{ - - switch (node) { - case (NODE_ROOT): - return(0); - case (NODE_ARG): - switch (parent) { - case (NODE_ARG): - case (NODE_CMDSYNOPSIS): - return(1); - default: - 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: - 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: - 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: - 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: - 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: - break; - } - return(0); - case (NODE_MANVOLNUM): - switch (parent) { - case (NODE_CITEREFENTRY): - case (NODE_REFMETA): - return(1); - default: - 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: - 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_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): - break; - } - - abort(); - return(0); -} - static void xml_char(void *arg, const XML_Char *p, int sz) { @@ -384,7 +171,7 @@ xml_char(void *arg, const XML_Char *p, int sz) * 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! + * process: strip out all leading whitespace to be sure. */ if (NODE_TEXT != ps->node) { for (i = 0; i < sz; i++) @@ -392,6 +179,8 @@ xml_char(void *arg, const XML_Char *p, int sz) break; if (i == sz) return; + p += i; + sz -= i; dat = calloc(1, sizeof(struct pnode)); if (NULL == dat) { perror(NULL); @@ -401,10 +190,10 @@ xml_char(void *arg, const XML_Char *p, int sz) dat->node = ps->node = NODE_TEXT; dat->parent = ps->cur; TAILQ_INIT(&dat->childq); + TAILQ_INIT(&dat->attrq); TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); ps->cur = dat; assert(NULL != ps->root); - } /* Append to current buffer. */ @@ -419,23 +208,40 @@ xml_char(void *arg, const XML_Char *p, int sz) ps->cur->bsz += (size_t)sz; } +static void +pnode_trim(struct pnode *pn) +{ + + assert(NODE_TEXT == pn->node); + for ( ; pn->bsz > 0; pn->bsz--) + if ( ! isspace((int)pn->b[pn->bsz - 1])) + break; +} + /* * 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 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're at the root and already have a tree, puke and exit. + * 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) { - struct parse *ps = arg; - enum nodeid node; - struct pnode *dat; + struct parse *ps = arg; + enum nodeid node; + enum attrkey key; + enum attrval val; + struct pnode *dat; + struct pattr *pattr; + const XML_Char **att; if (ps->stop) return; @@ -443,6 +249,7 @@ xml_elem_start(void *arg, const XML_Char *name, const /* 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; @@ -455,21 +262,30 @@ xml_elem_start(void *arg, const XML_Char *name, const break; 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); + fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", + ps->fname, XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), name); ps->stop = 1; return; } else if (NODE_ROOT == ps->node && NULL != ps->root) { - fprintf(stderr, "%s: reentering?\n", name); + fprintf(stderr, "%s:%zu:%zu: multiple refentries\n", + ps->fname, XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml)); 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); + fprintf(stderr, "%s:%zu:%zu: bad parent \"%s\" " + "of node \"%s\"\n", + ps->fname, XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + NULL == nodes[ps->node].name ? + "(none)" : nodes[ps->node].name, + NULL == nodes[node].name ? + "(none)" : nodes[node].name); ps->stop = 1; return; } @@ -482,6 +298,7 @@ xml_elem_start(void *arg, const XML_Char *name, const dat->node = ps->node = node; dat->parent = ps->cur; TAILQ_INIT(&dat->childq); + TAILQ_INIT(&dat->attrq); if (NULL != ps->cur) TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); @@ -489,11 +306,53 @@ xml_elem_start(void *arg, const XML_Char *name, const ps->cur = dat; if (NULL == ps->root) ps->root = dat; + + /* + * Process attributes. + */ + for (att = atts; NULL != *att; att += 2) { + for (key = 0; key < ATTRKEY__MAX; key++) + if (0 == strcmp(*att, attrkeys[key])) + break; + if (ATTRKEY__MAX == key) { + fprintf(stderr, "%s:%zu:%zu: unknown " + "attribute \"%s\"\n", ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *att); + continue; + } else if ( ! isattrkey(node, key)) { + fprintf(stderr, "%s:%zu:%zu: bad " + "attribute \"%s\"\n", ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *att); + continue; + } + for (val = 0; val < ATTRVAL__MAX; val++) + if (0 == strcmp(*(att + 1), attrvals[val])) + break; + if (ATTRVAL__MAX != val && ! isattrval(key, val)) { + fprintf(stderr, "%s:%zu:%zu: bad " + "value \"%s\"\n", ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *(att + 1)); + continue; + } + pattr = calloc(1, sizeof(struct pattr)); + pattr->key = key; + pattr->val = val; + if (ATTRVAL__MAX == val) + pattr->rawval = strdup(*(att + 1)); + TAILQ_INSERT_TAIL(&dat->attrq, pattr, child); + } + } /* * Roll up the parse tree. - * Does nothing else special. + * If we're at a text node, roll that one up first. * If we hit the root, then assign ourselves as the NODE_ROOT. */ static void @@ -507,6 +366,7 @@ xml_elem_end(void *arg, const XML_Char *name) /* 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; @@ -518,10 +378,14 @@ xml_elem_end(void *arg, const XML_Char *name) ps->node = ps->cur->node; } +/* + * Recursively free a node (NULL is ok). + */ static void pnode_free(struct pnode *pn) { struct pnode *pp; + struct pattr *ap; if (NULL == pn) return; @@ -531,10 +395,19 @@ pnode_free(struct pnode *pn) pnode_free(pp); } + while (NULL != (ap = TAILQ_FIRST(&pn->attrq))) { + TAILQ_REMOVE(&pn->attrq, ap, child); + free(ap->rawval); + free(ap); + } + free(pn->b); free(pn); } +/* + * Unlink a node from its parent and pnode_free() it. + */ static void pnode_unlink(struct pnode *pn) { @@ -544,6 +417,9 @@ pnode_unlink(struct pnode *pn) pnode_free(pn); } +/* + * Unlink all children of a node and pnode_free() them. + */ static void pnode_unlinksub(struct pnode *pn) { @@ -552,6 +428,9 @@ pnode_unlinksub(struct pnode *pn) pnode_unlink(TAILQ_FIRST(&pn->childq)); } +/* + * Reset the lookaside buffer. + */ static void bufclear(struct parse *p) { @@ -559,6 +438,11 @@ bufclear(struct parse *p) p->b[p->bsz = 0] = '\0'; } +/* + * 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) { @@ -576,6 +460,12 @@ bufappend(struct parse *p, struct pnode *pn) p->b[p->bsz] = '\0'; } +/* + * 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) { @@ -587,16 +477,20 @@ bufappend_r(struct parse *p, struct pnode *pn) bufappend_r(p, pp); } +#define MACROLINE_NORM 0 +#define MACROLINE_UPPER 1 /* - * Print text presumably on a macro line. - * Ignore any child macros. + * Recursively print text presumably on a macro line. * Convert all whitespace to regular spaces. */ static void -pnode_printmacrolinepart(struct parse *p, struct pnode *pn) +pnode_printmacrolinetext(struct parse *p, struct pnode *pn, int fl) { char *cp; + if (0 == p->newln) + putchar(' '); + bufclear(p); bufappend_r(p, pn); @@ -619,13 +513,23 @@ pnode_printmacrolinepart(struct parse *p, struct pnode ('\0' == *(cp + 3) || ' ' == *(cp + 3))))) fputs("\\&", stdout); - putchar(*cp); + if (MACROLINE_UPPER & fl) + putchar(toupper((int)*cp)); + else + putchar((int)*cp); /* If we're a character escape, escape us. */ if ('\\' == *cp) putchar('e'); } } +static void +pnode_printmacrolinepart(struct parse *p, struct pnode *pn) +{ + + pnode_printmacrolinetext(p, pn, 0); +} + /* * Just pnode_printmacrolinepart() but with a newline. * If no text, just the newline. @@ -634,24 +538,50 @@ static void pnode_printmacroline(struct parse *p, struct pnode *pn) { - pnode_printmacrolinepart(p, pn); + assert(0 == p->newln); + pnode_printmacrolinetext(p, pn, 0); putchar('\n'); + p->newln = 1; } static void +pnode_printmopen(struct parse *p) +{ + if (p->newln) { + putchar('.'); + p->newln = 0; + } else + putchar(' '); +} + +static void +pnode_printmclose(struct parse *p, int sv) +{ + + if (sv && ! p->newln) { + putchar('\n'); + p->newln = 1; + } +} + +/* + * If the SYNOPSIS macro has a superfluous title, kill it. + */ +static void pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn) { struct pnode *pp; - TAILQ_FOREACH(pp, &pn->childq, child) + TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TITLE == pp->node) { pnode_unlink(pp); - break; + return; } - - puts(".Sh SYNOPSIS"); } +/* + * Start a hopefully-named `Sh' section. + */ static void pnode_printrefsect(struct parse *p, struct pnode *pn) { @@ -661,39 +591,54 @@ pnode_printrefsect(struct parse *p, struct pnode *pn) if (NODE_TITLE == pp->node) break; - fputs(".Sh ", stdout); + if (NODE_REFSECT1 == pn->node) + fputs(".Sh", stdout); + else + fputs(".Ss", stdout); + p->newln = 0; + if (NULL != pp) { - pnode_printmacroline(p, pp); + pnode_printmacrolinetext(p, pp, + NODE_REFSECT1 == pn->node ? + MACROLINE_UPPER : 0); + pnode_printmclose(p, 1); pnode_unlink(pp); - } else + } else { puts("UNKNOWN"); + p->newln = 1; + } } +/* + * Start a reference, extracting the title and volume. + */ static void pnode_printciterefentry(struct parse *p, struct pnode *pn) { struct pnode *pp, *title, *manvol; title = manvol = NULL; + assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_MANVOLNUM == pp->node) manvol = pp; else if (NODE_REFENTRYTITLE == pp->node) title = pp; - fputs(".Xr ", stdout); + fputs(".Xr", stdout); + p->newln = 0; if (NULL != title) { pnode_printmacrolinepart(p, title); - putchar(' '); } else - fputs("unknown ", stdout); + fputs(" unknown ", stdout); - if (NULL != manvol) + if (NULL == manvol) { + puts(" 1"); + p->newln = 1; + } else pnode_printmacroline(p, manvol); - else - puts("1"); } static void @@ -702,6 +647,7 @@ pnode_printrefmeta(struct parse *p, struct pnode *pn) struct pnode *pp, *title, *manvol; title = manvol = NULL; + assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_MANVOLNUM == pp->node) manvol = pp; @@ -709,19 +655,19 @@ pnode_printrefmeta(struct parse *p, struct pnode *pn) title = pp; puts(".Dd $Mdocdate" "$"); - fputs(".Dt ", stdout); + fputs(".Dt", stdout); + p->newln = 0; - if (NULL != title) { - /* FIXME: uppercase. */ - pnode_printmacrolinepart(p, title); - putchar(' '); - } else - fputs("UNKNOWN ", stdout); + if (NULL != title) + pnode_printmacrolinetext(p, title, MACROLINE_UPPER); + else + fputs(" UNKNOWN ", stdout); - if (NULL != manvol) + if (NULL == manvol) { + puts(" 1"); + p->newln = 1; + } else pnode_printmacroline(p, manvol); - else - puts("1"); puts(".Os"); } @@ -731,6 +677,7 @@ pnode_printfuncdef(struct parse *p, struct pnode *pn) { struct pnode *pp, *ftype, *func; + assert(p->newln); ftype = func = NULL; TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TEXT == pp->node) @@ -739,15 +686,19 @@ pnode_printfuncdef(struct parse *p, struct pnode *pn) func = pp; if (NULL != ftype) { - fputs(".Ft ", stdout); + fputs(".Ft", stdout); + p->newln = 0; pnode_printmacroline(p, ftype); } if (NULL != func) { - fputs(".Fo ", stdout); + fputs(".Fo", stdout); + p->newln = 0; pnode_printmacroline(p, func); - } else + } else { puts(".Fo UNKNOWN"); + p->newln = 1; + } } static void @@ -755,6 +706,7 @@ pnode_printparamdef(struct parse *p, struct pnode *pn) { struct pnode *pp, *ptype, *param; + assert(p->newln); ptype = param = NULL; TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TEXT == pp->node) @@ -763,6 +715,7 @@ pnode_printparamdef(struct parse *p, struct pnode *pn) param = pp; fputs(".Fa \"", stdout); + p->newln = 0; if (NULL != ptype) { pnode_printmacrolinepart(p, ptype); putchar(' '); @@ -770,10 +723,9 @@ pnode_printparamdef(struct parse *p, struct pnode *pn) if (NULL != param) pnode_printmacrolinepart(p, param); - else - fputs("UNKNOWN", stdout); puts("\""); + p->newln = 1; } static void @@ -781,6 +733,7 @@ pnode_printfuncprototype(struct parse *p, struct pnode { struct pnode *pp, *fdef; + assert(p->newln); TAILQ_FOREACH(fdef, &pn->childq, child) if (NODE_FUNCDEF == fdef->node) break; @@ -795,30 +748,48 @@ pnode_printfuncprototype(struct parse *p, struct pnode pnode_printparamdef(p, pp); puts(".Fc"); + p->newln = 1; } -/* TODO: handle "optional" values. */ +/* + * 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 -pnode_printarg(struct parse *p, struct pnode *pn, int nested) +pnode_printarg(struct parse *p, struct pnode *pn) { struct pnode *pp; - int sv = nested; + struct pattr *ap; + int isop, isrep; - if ( ! nested) - fputs(".", stdout); - nested = 1; - TAILQ_FOREACH(pp, &pn->childq, child) - if (NODE_OPTION == pp->node) { - fputs("Fl ", stdout); - pnode_printmacrolinepart(p, pp); - } else if (NODE_TEXT == pp->node) { - fputs("Ar ", stdout); - pnode_printmacrolinepart(p, pp); - } else if (NODE_ARG == pp->node) - pnode_printarg(p, pp, nested); + isop = 1; + isrep = 0; + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ATTRKEY_CHOICE == ap->key && + (ATTRVAL_PLAIN == ap->val || + ATTRVAL_REQ == ap->val)) + isop = 0; + else if (ATTRKEY_REP == ap->key && + (ATTRVAL_REPEAT == ap->val)) + isrep = 1; - if ( ! sv) - puts(""); + if (isop) { + pnode_printmopen(p); + fputs("Op", stdout); + } + + TAILQ_FOREACH(pp, &pn->childq, child) { + if (NODE_TEXT == pp->node) { + pnode_printmopen(p); + fputs("Ar", stdout); + } + pnode_print(p, pp); + if (NODE_TEXT == pp->node && isrep) + fputs("...", stdout); + } } /* @@ -845,7 +816,10 @@ pnode_printprologue(struct parse *p, struct pnode *pn) { struct pnode *pp; - if (NULL != (pp = pnode_findfirst(p->root, NODE_REFMETA))) { + pp = NULL == p->root ? NULL : + pnode_findfirst(p->root, NODE_REFMETA); + + if (NULL != pp) { pnode_printrefmeta(p, pp); pnode_unlink(pp); } else { @@ -856,93 +830,230 @@ pnode_printprologue(struct parse *p, struct pnode *pn) } } +static void +pnode_printvarlistentry(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + assert(p->newln); + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_TERM == pp->node) { + fputs(".It", stdout); + p->newln = 0; + pnode_print(p, pp); + pnode_unlink(pp); + pnode_printmclose(p, 1); + return; + } + + puts(".It"); + p->newln = 1; +} + +static void +pnode_printitemizedlist(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + assert(p->newln); + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_TITLE == pp->node) { + puts(".Pp"); + pnode_print(p, pp); + pnode_unlink(pp); + } + + assert(p->newln); + + if (NODE_ORDEREDLIST == pn->node) + puts(".Bl -enum"); + else + puts(".Bl -item"); + + TAILQ_FOREACH(pp, &pn->childq, child) { + assert(p->newln); + puts(".It"); + pnode_print(p, pp); + pnode_printmclose(p, 1); + } + assert(p->newln); + puts(".El"); +} + +static void +pnode_printvariablelist(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + assert(p->newln); + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_TITLE == pp->node) { + puts(".Pp"); + pnode_print(p, pp); + pnode_unlink(pp); + } + + assert(p->newln); + puts(".Bl -tag -width Ds"); + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_VARLISTENTRY != pp->node) { + assert(p->newln); + fputs(".It", stdout); + pnode_printmacroline(p, pp); + } else { + assert(p->newln); + pnode_print(p, pp); + } + assert(p->newln); + puts(".El"); +} + /* * 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) { struct pnode *pp; char *cp; - int last; + int last, sv; if (NULL == pn) return; - if (NODE_TEXT != pn->node && NODE_ROOT != pn->node) - printf(".\\\" %s\n", nodes[pn->node].name); + sv = p->newln; switch (pn->node) { case (NODE_ARG): - pnode_printarg(p, pn, 0); + pnode_printarg(p, pn); pnode_unlinksub(pn); break; case (NODE_CITEREFENTRY): + assert(p->newln); pnode_printciterefentry(p, pn); pnode_unlinksub(pn); break; case (NODE_CODE): - fputs(".Li ", stdout); - pnode_printmacroline(p, pn); - pnode_unlinksub(pn); + pnode_printmopen(p); + fputs("Li", stdout); break; case (NODE_COMMAND): - fputs(".Nm ", stdout); - pnode_printmacroline(p, pn); - pnode_unlinksub(pn); + pnode_printmopen(p); + fputs("Nm", stdout); break; + case (NODE_EMPHASIS): + pnode_printmopen(p); + fputs("Em", stdout); + break; + case (NODE_ENVAR): + pnode_printmopen(p); + fputs("Ev", stdout); + break; + case (NODE_FILENAME): + pnode_printmopen(p); + fputs("Pa", stdout); + break; case (NODE_FUNCTION): - fputs(".Fn ", stdout); - pnode_printmacroline(p, pn); - pnode_unlinksub(pn); + pnode_printmopen(p); + fputs("Fn", stdout); break; case (NODE_FUNCPROTOTYPE): + assert(p->newln); pnode_printfuncprototype(p, pn); pnode_unlinksub(pn); break; case (NODE_FUNCSYNOPSISINFO): - fputs(".Fd ", stdout); - pnode_printmacroline(p, pn); - pnode_unlinksub(pn); + pnode_printmopen(p); + fputs("Fd", stdout); break; + case (NODE_ITEMIZEDLIST): + /* FALLTHROUGH */ + case (NODE_ORDEREDLIST): + assert(p->newln); + pnode_printitemizedlist(p, pn); + break; + case (NODE_LITERAL): + pnode_printmopen(p); + fputs("Li", stdout); + break; + case (NODE_OPTION): + pnode_printmopen(p); + fputs("Fl", stdout); + break; case (NODE_PARA): - /* FIXME: not always. */ + assert(p->newln); + if (NULL != pn->parent && + NODE_LISTITEM == pn->parent->node) + break; puts(".Pp"); break; case (NODE_PARAMETER): - fputs(".Fa \"", stdout); + /* Suppress non-text children... */ + pnode_printmopen(p); + fputs("Fa \"", stdout); pnode_printmacrolinepart(p, pn); puts("\""); pnode_unlinksub(pn); break; case (NODE_PROGRAMLISTING): + /* FALLTHROUGH */ + case (NODE_SCREEN): + assert(p->newln); puts(".Bd -literal"); break; + case (NODE_REFENTRYINFO): + /* Suppress. */ + pnode_unlinksub(pn); + break; case (NODE_REFMETA): abort(); break; case (NODE_REFNAME): - fputs(".Nm ", stdout); - pnode_printmacroline(p, pn); + /* Suppress non-text children... */ + pnode_printmopen(p); + fputs("Nm", stdout); + p->newln = 0; + pnode_printmacrolinepart(p, pn); pnode_unlinksub(pn); - return; + break; case (NODE_REFNAMEDIV): + assert(p->newln); puts(".Sh NAME"); break; case (NODE_REFPURPOSE): - fputs(".Nd ", stdout); - pnode_printmacroline(p, pn); - pnode_unlinksub(pn); - return; + assert(p->newln); + pnode_printmopen(p); + fputs("Nd", stdout); + break; case (NODE_REFSYNOPSISDIV): + assert(p->newln); pnode_printrefsynopsisdiv(p, pn); + puts(".Sh SYNOPSIS"); break; case (NODE_REFSECT1): + /* FALLTHROUGH */ + case (NODE_REFSECT2): + assert(p->newln); pnode_printrefsect(p, pn); break; + case (NODE_REPLACEABLE): + pnode_printmopen(p); + fputs("Ar", stdout); + break; + case (NODE_SBR): + assert(p->newln); + puts(".br"); + break; + case (NODE_STRUCTNAME): + pnode_printmopen(p); + fputs("Vt", stdout); + break; case (NODE_TEXT): + if (0 == p->newln) + putchar(' '); bufclear(p); bufappend(p, pn); /* @@ -951,7 +1062,18 @@ pnode_print(struct parse *p, struct pnode *pn) * XXX: all whitespace, including tabs (?). * Remember to escape control characters and escapes. */ - for (last = '\n', cp = p->b; '\0' != *cp; ) { + assert(p->bsz); + cp = p->b; + /* + * There's often a superfluous "-" in its