=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.10 retrieving revision 1.93 diff -u -p -r1.10 -r1.93 --- docbook2mdoc/docbook2mdoc.c 2014/03/29 10:56:21 1.10 +++ docbook2mdoc/docbook2mdoc.c 2019/04/07 13:16:21 1.93 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.10 2014/03/29 10:56:21 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,1073 +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 */ - 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 void +pnode_printtext(struct format *f, struct pnode *n) +{ + char *cp; + char last; -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 }, -}; + if (n->bsz == 0) { + assert(n->real < n->b); + return; + } -static void -pnode_print(struct parse *p, struct pnode *pn); + if (f->linestate == LINE_NEW) { + last = '\n'; + f->linestate = LINE_TEXT; + } else { + last = ' '; + if (n->spc || f->linestate == LINE_MACRO) + putchar(' '); + } -/* - * 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) -{ + if (n->node == NODE_ESCAPE) { + fputs(n->b, stdout); + return; + } - switch (node) { - case (NODE_ROOT): - return(0); - case (NODE_ARG): - switch (parent) { - case (NODE_ARG): - case (NODE_CMDSYNOPSIS): - return(1); - default: - break; + /* + * Remove the prefix '-' from