=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.67 retrieving revision 1.109 diff -u -p -r1.67 -r1.109 --- docbook2mdoc/docbook2mdoc.c 2019/03/23 10:53:52 1.67 +++ docbook2mdoc/docbook2mdoc.c 2019/04/12 10:34:48 1.109 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.67 2019/03/23 10:53:52 schwarze Exp $ */ +/* $Id: docbook2mdoc.c,v 1.109 2019/04/12 10:34:48 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -15,754 +15,175 @@ * 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 -#include "extern.h" +#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 { - XML_Parser xml; - enum nodeid node; /* current (NODE_ROOT if pre-tree) */ - const char *fname; /* filename */ - int stop; /* should we stop now? */ -#define PARSE_EQN 1 - unsigned int flags; /* document-wide flags */ - struct pnode *root; /* root of parse tree */ - struct pnode *cur; /* current node in tree */ - char *b; /* NUL-terminated buffer for pre-print */ - size_t bsz; /* current length of b */ - size_t mbsz; /* max bsz allocation */ - int level; /* header level, starting at 1 */ - int newln; /* output: are we on a fresh line */ -}; -struct node { - const char *name; /* docbook element name */ - enum nodeid node; /* docbook element to generate */ -}; +static void pnode_print(struct format *, struct pnode *); -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 */ - char *real; /* store for "b" */ - 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", - "class", - "close", - "id", - "linkend", - "open", - "rep" -}; - -static const char *attrvals[ATTRVAL__MAX] = { - "monospaced", - "norepeat", - "opt", - "plain", - "repeat", - "req" -}; - -static const struct node nodes[] = { - { "acronym", NODE_ACRONYM }, - { "affiliation", NODE_AFFILIATION }, - { "anchor", NODE_ANCHOR }, - { "application", NODE_APPLICATION }, - { "arg", NODE_ARG }, - { "author", NODE_AUTHOR }, - { "authorgroup", NODE_AUTHORGROUP }, - { "blockquote", NODE_BLOCKQUOTE }, - { "book", NODE_BOOK }, - { "bookinfo", NODE_BOOKINFO }, - { "caution", NODE_CAUTION }, - { "chapter", NODE_SECTION }, - { "citerefentry", NODE_CITEREFENTRY }, - { "citetitle", NODE_CITETITLE }, - { "cmdsynopsis", NODE_CMDSYNOPSIS }, - { "code", NODE_CODE }, - { "colspec", NODE_COLSPEC }, - { "command", NODE_COMMAND }, - { "constant", NODE_CONSTANT }, - { "copyright", NODE_COPYRIGHT }, - { "date", NODE_DATE }, - { "editor", NODE_EDITOR }, - { "email", NODE_EMAIL }, - { "emphasis", NODE_EMPHASIS }, - { "entry", NODE_ENTRY }, - { "envar", NODE_ENVAR }, - { "fieldsynopsis", NODE_FIELDSYNOPSIS }, - { "filename", NODE_FILENAME }, - { "firstname", NODE_FIRSTNAME }, - { "firstterm", NODE_FIRSTTERM }, - { "footnote", NODE_FOOTNOTE }, - { "funcdef", NODE_FUNCDEF }, - { "funcprototype", NODE_FUNCPROTOTYPE }, - { "funcsynopsis", NODE_FUNCSYNOPSIS }, - { "funcsynopsisinfo", NODE_FUNCSYNOPSISINFO }, - { "function", NODE_FUNCTION }, - { "glossterm", NODE_GLOSSTERM }, - { "group", NODE_GROUP }, - { "holder", NODE_HOLDER }, - { "index", NODE_INDEX }, - { "indexterm", NODE_INDEXTERM }, - { "info", NODE_INFO }, - { "informalequation", NODE_INFORMALEQUATION }, - { "informaltable", NODE_INFORMALTABLE }, - { "inlineequation", NODE_INLINEEQUATION }, - { "itemizedlist", NODE_ITEMIZEDLIST }, - { "keysym", NODE_KEYSYM }, - { "legalnotice", NODE_LEGALNOTICE }, - { "link", NODE_LINK }, - { "listitem", NODE_LISTITEM }, - { "literal", NODE_LITERAL }, - { "literallayout", NODE_LITERALLAYOUT }, - { "manvolnum", NODE_MANVOLNUM }, - { "member", NODE_MEMBER }, - { "mml:math", NODE_MML_MATH }, - { "mml:mfenced", NODE_MML_MFENCED }, - { "mml:mfrac", NODE_MML_MFRAC }, - { "mml:mi", NODE_MML_MI }, - { "mml:mn", NODE_MML_MN }, - { "mml:mo", NODE_MML_MO }, - { "mml:mrow", NODE_MML_MROW }, - { "mml:msub", NODE_MML_MSUB }, - { "mml:msup", NODE_MML_MSUP }, - { "modifier", NODE_MODIFIER }, - { "note", NODE_NOTE }, - { "option", NODE_OPTION }, - { "orderedlist", NODE_ORDEREDLIST }, - { "orgname", NODE_ORGNAME }, - { "othername", NODE_OTHERNAME }, - { "para", NODE_PARA }, - { "paramdef", NODE_PARAMDEF }, - { "parameter", NODE_PARAMETER }, - { "part", NODE_SECTION }, - { "personname", NODE_PERSONNAME }, - { "phrase", NODE_PHRASE }, - { "preface", NODE_PREFACE }, - { "primary", NODE_PRIMARY }, - { "programlisting", NODE_PROGRAMLISTING }, - { "prompt", NODE_PROMPT }, - { "quote", NODE_QUOTE }, - { "refclass", NODE_REFCLASS }, - { "refdescriptor", NODE_REFDESCRIPTOR }, - { "refentry", NODE_REFENTRY }, - { "refentryinfo", NODE_REFENTRYINFO }, - { "refentrytitle", NODE_REFENTRYTITLE }, - { "refmeta", NODE_REFMETA }, - { "refmetainfo", NODE_REFMETAINFO }, - { "refmiscinfo", NODE_REFMISCINFO }, - { "refname", NODE_REFNAME }, - { "refnamediv", NODE_REFNAMEDIV }, - { "refpurpose", NODE_REFPURPOSE }, - { "refsect1", NODE_SECTION }, - { "refsect2", NODE_SECTION }, - { "refsect3", NODE_SECTION }, - { "refsection", NODE_SECTION }, - { "refsynopsisdiv", NODE_REFSYNOPSISDIV }, - { "releaseinfo", NODE_RELEASEINFO }, - { "replaceable", NODE_REPLACEABLE }, - { "row", NODE_ROW }, - { "sbr", NODE_SBR }, - { "screen", NODE_SCREEN }, - { "secondary", NODE_SECONDARY }, - { "sect1", NODE_SECTION }, - { "sect2", NODE_SECTION }, - { "section", NODE_SECTION }, - { "sgmltag", NODE_SGMLTAG }, - { "simplelist", NODE_SIMPLELIST }, - { "spanspec", NODE_SPANSPEC }, - { "structname", NODE_STRUCTNAME }, - { "subtitle", NODE_SUBTITLE }, - { "surname", NODE_SURNAME }, - { "synopsis", NODE_SYNOPSIS }, - { "table", NODE_TABLE }, - { "tbody", NODE_TBODY }, - { "term", NODE_TERM }, - { "tfoot", NODE_TFOOT }, - { "tgroup", NODE_TGROUP }, - { "thead", NODE_THEAD }, - { "tip", NODE_TIP }, - { "title", NODE_TITLE }, - { "trademark", NODE_TRADEMARK }, - { "type", NODE_TYPE }, - { "ulink", NODE_ULINK }, - { "userinput", NODE_USERINPUT }, - { "variablelist", NODE_VARIABLELIST }, - { "varlistentry", NODE_VARLISTENTRY }, - { "varname", NODE_VARNAME }, - { "warning", NODE_WARNING }, - { "wordasword", NODE_WORDASWORD }, - { "year", NODE_YEAR }, - { NULL, NODE__MAX } -}; - -static int warn = 0; - static void -pnode_print(struct parse *p, struct pnode *pn); - -/* - * 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 void -xml_char(void *arg, const XML_Char *p, int sz) +pnode_printtext(struct format *f, struct pnode *n) { - struct parse *ps = arg; - struct pnode *dat; - int i; + struct pnode *nn; + char *cp; + int accept_arg; - /* Stopped or no tree yet. */ - if (ps->stop || ps->node == NODE_ROOT) - return; + cp = n->b; + accept_arg = f->flags & FMT_ARG; + if (f->linestate == LINE_MACRO && !n->spc && !accept_arg) { + for (;;) { + if (*cp == '\0') + return; + if (strchr("!),.:;?]", *cp) == NULL) + break; + printf(" %c", *cp++); + } + if (isspace((unsigned char)*cp)) { + while (isspace((unsigned char)*cp)) + cp++; + macro_close(f); + } else { + fputs(" Ns", stdout); + f->flags &= FMT_IMPL; + accept_arg = 1; + } + } + if (f->linestate == LINE_MACRO && !accept_arg && + (f->flags & (FMT_CHILD | FMT_IMPL)) == 0) + macro_close(f); - assert(ps->cur != NULL); - /* - * 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. + * Text preceding a macro without intervening whitespace + * requires a .Pf macro. + * Set the spacing flag to avoid a redundant .Ns macro. */ - if (ps->node != NODE_TEXT) { - for (i = 0; i < sz; i++) - if ( ! isspace((unsigned char)p[i])) - break; - if (i == sz) - return; - p += i; - sz -= i; - dat = calloc(1, sizeof(struct pnode)); - if (dat == NULL) { - perror(NULL); - exit(1); - } - 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(ps->root != NULL); + if (f->linestate != LINE_MACRO && + (nn = TAILQ_NEXT(n, child)) != NULL && nn->spc == 0 && + (nn->node != NODE_TEXT && nn->node != NODE_ESCAPE)) { + macro_open(f, "Pf"); + accept_arg = 1; + f->flags |= FMT_CHILD; + nn->spc = 1; } - /* Append to current buffer. */ - assert(sz >= 0); - ps->cur->b = realloc(ps->cur->b, - ps->cur->bsz + (size_t)sz); - if (ps->cur->b == NULL) { - perror(NULL); - exit(1); + switch (f->linestate) { + case LINE_NEW: + break; + case LINE_TEXT: + if (n->spc) { + if (n->node == NODE_TEXT) + macro_close(f); + else + putchar(' '); + } + break; + case LINE_MACRO: + if (accept_arg) + putchar(' '); + else + macro_close(f); + break; } - memcpy(ps->cur->b + ps->cur->bsz, p, sz); - ps->cur->bsz += (size_t)sz; - ps->cur->real = ps->cur->b; -} -static void -pnode_trim(struct pnode *pn) -{ - assert(pn->node == NODE_TEXT); - for ( ; pn->bsz > 0; pn->bsz--) - if ( ! isspace((unsigned char)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 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) -{ - struct parse *ps = arg; - const struct node *node; - enum attrkey key; - enum attrval val; - struct pnode *dat; - struct pattr *pattr; - const XML_Char **att; - - /* FIXME: find a better way to ditch other namespaces. */ - if (ps->stop || strcmp(name, "xi:include") == 0) + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; return; - - /* Close out text node, if applicable... */ - if (ps->node == NODE_TEXT) { - pnode_trim(ps->cur); - ps->cur = ps->cur->parent; - ps->node = ps->cur->node; } - for (node = nodes; node->name != NULL; node++) - if (strcmp(node->name, name) == 0) - break; - - if (node->name == NULL) { - if (ps->node == NODE_ROOT) - return; - 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 (ps->node == NODE_ROOT && ps->root != NULL) { - fprintf(stderr, "%s:%zu:%zu: multiple refentries\n", - ps->fname, XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml)); - ps->stop = 1; - return; - } - - if (node->node == NODE_INLINEEQUATION) - ps->flags |= PARSE_EQN; - - if ((dat = calloc(1, sizeof(struct pnode))) == NULL) { - perror(NULL); - exit(1); - } - - dat->node = ps->node = node->node; - dat->parent = ps->cur; - TAILQ_INIT(&dat->childq); - TAILQ_INIT(&dat->attrq); - - if (ps->cur != NULL) - TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); - - ps->cur = dat; - if (ps->root == NULL) - ps->root = dat; - /* - * Process attributes. + * Remove the prefix '-' from