=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.9 retrieving revision 1.93 diff -u -p -r1.9 -r1.93 --- docbook2mdoc/docbook2mdoc.c 2014/03/28 13:16:40 1.9 +++ docbook2mdoc/docbook2mdoc.c 2019/04/07 13:16:21 1.93 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.9 2014/03/28 13:16:40 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.93 2019/04/07 13:16:21 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,1034 +15,877 @@ * 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 */ -}; -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 }, -}; - -/* - * 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) +static void +pnode_printtext(struct format *f, struct pnode *n) { + char *cp; + char last; - 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_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; + if (n->bsz == 0) { + assert(n->real < n->b); + return; } - abort(); - return(0); -} + if (f->linestate == LINE_NEW) { + last = '\n'; + f->linestate = LINE_TEXT; + } else { + last = ' '; + if (n->spc || f->linestate == LINE_MACRO) + putchar(' '); + } -/* - * 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) -{ - struct parse *ps = arg; - struct pnode *dat; - int i; - - /* Stopped or no tree yet. */ - if (ps->stop || NODE_ROOT == ps->node) + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); 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! + * Remove the prefix '-' from