=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.23 retrieving revision 1.148 diff -u -p -r1.23 -r1.148 --- docbook2mdoc/docbook2mdoc.c 2014/03/30 17:46:17 1.23 +++ docbook2mdoc/docbook2mdoc.c 2019/05/02 04:15:40 1.148 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.23 2014/03/30 17:46:17 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.148 2019/05/02 04:15:40 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,898 +15,915 @@ * 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 "xmalloc.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? */ - 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); -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", 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); - -/* - * 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 || NODE_ROOT == ps->node) - return; + para_check(f); + cp = n->b; + accept_arg = f->flags & FMT_ARG; + if (f->linestate == LINE_MACRO && !accept_arg && + (n->flags & NFLAG_SPC) == 0) { + for (;;) { + if (*cp == '\0') + return; + if (strchr("!),.:;?]", *cp) == NULL) + break; + printf(" %c", *cp++); + } + if (isspace((unsigned char)*cp)) { + while (isspace((unsigned char)*cp)) + cp++; + n->flags |= NFLAG_SPC; + } else { + f->flags &= ~FMT_NOSPC; + f->flags |= FMT_CHILD; + macro_open(f, "Ns"); + f->flags &= ~FMT_ARG; + f->flags |= FMT_CHILD; + accept_arg = 1; + } + } + if (f->linestate == LINE_MACRO && f->nofill == 0 && + !accept_arg && (f->flags & FMT_IMPL) == 0) + macro_close(f); - /* 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. + * Text preceding a macro without intervening whitespace + * requires a .Pf macro. + * Set the spacing flag to avoid a redundant .Ns macro. */ - 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); + + if ((f->nofill || f->linestate != LINE_MACRO) && + (nn = TAILQ_NEXT(n, child)) != NULL && + (nn->flags & NFLAG_SPC) == 0) { + switch (pnode_class(nn->node)) { + case CLASS_LINE: + case CLASS_ENCL: + macro_open(f, "Pf"); + accept_arg = 1; + f->flags |= FMT_CHILD; + nn->flags |= NFLAG_SPC; + break; + default: + break; } + } - 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); + switch (f->linestate) { + case LINE_NEW: + break; + case LINE_TEXT: + if (n->flags & NFLAG_SPC) { + if (n->flags & NFLAG_LINE && + pnode_class(n->node) == CLASS_TEXT) + macro_close(f); + else + putchar(' '); + } + break; + case LINE_MACRO: + if (accept_arg == 0) { + if (f->nofill) { + f->flags &= ~FMT_NOSPC; + f->flags |= FMT_CHILD; + macro_open(f, "No "); + f->flags &= ~FMT_ARG; + f->flags |= FMT_CHILD; + } else + macro_close(f); + } else if (n->flags & NFLAG_SPC || + (f->flags & FMT_ARG) == 0 || + (nn = TAILQ_PREV(n, pnodeq, child)) == NULL || + pnode_class(nn->node) != CLASS_TEXT) + putchar(' '); + break; } - /* 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); + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; + return; } - memcpy(ps->cur->b + ps->cur->bsz, p, sz); - ps->cur->bsz += (size_t)sz; + + /* + * Remove the prefix '-' from