=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.10 retrieving revision 1.109 diff -u -p -r1.10 -r1.109 --- docbook2mdoc/docbook2mdoc.c 2014/03/29 10:56:21 1.10 +++ docbook2mdoc/docbook2mdoc.c 2019/04/12 10:34:48 1.109 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.10 2014/03/29 10:56:21 kristaps 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 * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,1195 +15,1116 @@ * 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 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 }, -}; - static void -pnode_print(struct parse *p, struct pnode *pn); - -/* - * 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. - */ -static int -isparent(enum nodeid node, enum nodeid parent) +pnode_printtext(struct format *f, struct pnode *n) { + struct pnode *nn; + char *cp; + int accept_arg; - switch (node) { - case (NODE_ROOT): - return(0); - case (NODE_ARG): - switch (parent) { - case (NODE_ARG): - case (NODE_CMDSYNOPSIS): - return(1); - default: - break; + 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++); } - 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; + 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; } - return(0); - case (NODE_CMDSYNOPSIS): - switch (parent) { - case (NODE_PARA): - case (NODE_REFSECT1): - case (NODE_REFSYNOPSISDIV): - return(1); - default: - break; + } + if (f->linestate == LINE_MACRO && !accept_arg && + (f->flags & (FMT_CHILD | FMT_IMPL)) == 0) + macro_close(f); + + /* + * Text preceding a macro without intervening whitespace + * requires a .Pf macro. + * Set the spacing flag to avoid a redundant .Ns macro. + */ + + 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; + } + + switch (f->linestate) { + case LINE_NEW: + break; + case LINE_TEXT: + if (n->spc) { + if (n->node == NODE_TEXT) + macro_close(f); + else + putchar(' '); } - 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_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): break; + case LINE_MACRO: + if (accept_arg) + putchar(' '); + else + macro_close(f); + break; } - abort(); - return(0); + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + if (f->linestate == LINE_NEW) + f->linestate = LINE_TEXT; + return; + } + + /* + * Remove the prefix '-' from