version 1.62, 2019/03/22 17:42:53 |
version 1.96, 2019/04/07 17:00:56 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
#include <sys/queue.h> |
|
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <expat.h> |
|
#include <fcntl.h> |
|
#include <getopt.h> |
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
|
#include <unistd.h> |
|
|
|
#include "extern.h" |
#include "node.h" |
|
#include "macro.h" |
|
#include "format.h" |
|
|
/* |
/* |
* Global parse state. |
* The implementation of the mdoc(7) formatter. |
* Keep this as simple and small as possible. |
|
*/ |
*/ |
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 { |
static void pnode_print(struct format *, struct pnode *); |
const char *name; /* docbook element name */ |
|
enum nodeid node; /* docbook element to generate */ |
|
}; |
|
|
|
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 }, |
|
{ "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 }, |
|
{ "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 |
static void |
pnode_print(struct parse *p, struct pnode *pn); |
pnode_printtext(struct format *f, struct pnode *n) |
|
|
/* |
|
* 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 *nn; |
struct pnode *dat; |
char *cp; |
int i; |
char last; |
|
|
/* Stopped or no tree yet. */ |
if (n->bsz == 0) { |
if (ps->stop || NODE_ROOT == ps->node) |
assert(n->real < n->b); |
return; |
return; |
|
} |
|
|
assert(NULL != ps->cur); |
|
|
|
/* |
/* |
* Are we in the midst of processing text? |
* Text preceding a macro without intervening whitespace |
* If we're not processing text right now, then create a text |
* requires a .Pf macro. |
* node for doing so. |
* Set the spacing flag to avoid a redundant .Ns macro. |
* However, don't do so unless we have some non-whitespace to |
|
* process: strip out all leading whitespace to be sure. |
|
*/ |
*/ |
if (NODE_TEXT != ps->node) { |
|
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 (NULL == dat) { |
|
perror(NULL); |
|
exit(EXIT_FAILURE); |
|
} |
|
|
|
dat->node = ps->node = NODE_TEXT; |
if (f->linestate != LINE_MACRO && |
dat->parent = ps->cur; |
(nn = TAILQ_NEXT(n, child)) != NULL && nn->spc == 0 && |
TAILQ_INIT(&dat->childq); |
(nn->node != NODE_TEXT && nn->node != NODE_ESCAPE)) { |
TAILQ_INIT(&dat->attrq); |
macro_open(f, "Pf"); |
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
nn->spc = 1; |
ps->cur = dat; |
|
assert(NULL != ps->root); |
|
} |
} |
|
|
/* Append to current buffer. */ |
if (f->linestate == LINE_NEW) { |
assert(sz >= 0); |
last = '\n'; |
ps->cur->b = realloc(ps->cur->b, |
f->linestate = LINE_TEXT; |
ps->cur->bsz + (size_t)sz); |
} else { |
if (NULL == ps->cur->b) { |
last = ' '; |
perror(NULL); |
if (n->spc || f->linestate == LINE_MACRO) |
exit(EXIT_FAILURE); |
putchar(' '); |
} |
} |
memcpy(ps->cur->b + ps->cur->bsz, p, sz); |
|
ps->cur->bsz += (size_t)sz; |
|
ps->cur->real = ps->cur->b; |
|
} |
|
|
|
static void |
if (n->node == NODE_ESCAPE) { |
pnode_trim(struct pnode *pn) |
fputs(n->b, stdout); |
{ |
|
|
|
assert(NODE_TEXT == pn->node); |
|
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 || 0 == strcmp(name, "xi:include")) |
|
return; |
return; |
|
|
/* Close out text node, if applicable... */ |
|
if (NODE_TEXT == ps->node) { |
|
assert(NULL != ps->cur); |
|
pnode_trim(ps->cur); |
|
ps->cur = ps->cur->parent; |
|
assert(NULL != ps->cur); |
|
ps->node = ps->cur->node; |
|
} |
} |
|
|
for (node = nodes; NULL != node->name; node++) |
/* |
if (0 == strcmp(node->name, name)) |
* Remove the prefix '-' from <option> elements |
break; |
* because the arguments of .Fl macros do not need it. |
|
*/ |
|
|
if (NULL == node->name) { |
cp = n->b; |
if (NODE_ROOT == ps->node) |
if (n->parent != NULL && n->parent->node == NODE_OPTION && *cp == '-') |
return; |
cp++; |
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 (NODE_ROOT == ps->node && NULL != ps->root) { |
|
fprintf(stderr, "%s:%zu:%zu: multiple refentries\n", |
|
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml)); |
|
ps->stop = 1; |
|
return; |
|
} |
|
|
|
if (NODE_INLINEEQUATION == node->node) |
|
ps->flags |= PARSE_EQN; |
|
|
|
if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { |
|
perror(NULL); |
|
exit(EXIT_FAILURE); |
|
} |
|
|
|
dat->node = ps->node = node->node; |
|
dat->parent = ps->cur; |
|
TAILQ_INIT(&dat->childq); |
|
TAILQ_INIT(&dat->attrq); |
|
|
|
if (NULL != ps->cur) |
|
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
|
|
|
ps->cur = dat; |
|
if (NULL == ps->root) |
|
ps->root = dat; |
|
|
|
/* |
/* |
* Process attributes. |
* Print the text, skipping whitespace on new lines, |
|
* escaping control characters on new lines, |
|
* and escaping backslashes. |
*/ |
*/ |
for (att = atts; NULL != *att; att += 2) { |
|
for (key = 0; key < ATTRKEY__MAX; key++) |
|
if (0 == strcmp(*att, attrkeys[key])) |
|
break; |
|
if (ATTRKEY__MAX == key) { |
|
if (warn) |
|
fprintf(stderr, "%s:%zu:%zu: warning: " |
|
"unknown attribute \"%s\"\n", |
|
ps->fname, |
|
XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml), |
|
*att); |
|
continue; |
|
} |
|
for (val = 0; val < ATTRVAL__MAX; val++) |
|
if (0 == strcmp(*(att + 1), attrvals[val])) |
|
break; |
|
pattr = calloc(1, sizeof(struct pattr)); |
|
pattr->key = key; |
|
pattr->val = val; |
|
if (ATTRVAL__MAX == val) |
|
pattr->rawval = strdup(*(att + 1)); |
|
TAILQ_INSERT_TAIL(&dat->attrq, pattr, child); |
|
} |
|
|
|
} |
for (; *cp != '\0'; cp++) { |
|
if (last == '\n') { |
/* |
if (isspace((unsigned char)*cp)) |
* Roll up the parse tree. |
continue; |
* If we're at a text node, roll that one up first. |
if (*cp == '\'' || *cp == '.') |
* If we hit the root, then assign ourselves as the NODE_ROOT. |
fputs("\\&", stdout); |
*/ |
|
static void |
|
xml_elem_end(void *arg, const XML_Char *name) |
|
{ |
|
struct parse *ps = arg; |
|
|
|
/* FIXME: find a better way to ditch other namespaces. */ |
|
if (ps->stop || NODE_ROOT == ps->node) |
|
return; |
|
else if (0 == strcmp(name, "xi:include")) |
|
return; |
|
|
|
/* Close out text node, if applicable... */ |
|
if (NODE_TEXT == ps->node) { |
|
assert(NULL != ps->cur); |
|
pnode_trim(ps->cur); |
|
ps->cur = ps->cur->parent; |
|
assert(NULL != ps->cur); |
|
ps->node = ps->cur->node; |
|
} |
|
|
|
if (NULL == (ps->cur = ps->cur->parent)) |
|
ps->node = NODE_ROOT; |
|
else |
|
ps->node = ps->cur->node; |
|
} |
|
|
|
/* |
|
* Recursively free a node (NULL is ok). |
|
*/ |
|
static void |
|
pnode_free(struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
struct pattr *ap; |
|
|
|
if (NULL == pn) |
|
return; |
|
|
|
while (NULL != (pp = TAILQ_FIRST(&pn->childq))) { |
|
TAILQ_REMOVE(&pn->childq, pp, child); |
|
pnode_free(pp); |
|
} |
|
|
|
while (NULL != (ap = TAILQ_FIRST(&pn->attrq))) { |
|
TAILQ_REMOVE(&pn->attrq, ap, child); |
|
free(ap->rawval); |
|
free(ap); |
|
} |
|
|
|
free(pn->real); |
|
free(pn); |
|
} |
|
|
|
/* |
|
* Unlink a node from its parent and pnode_free() it. |
|
*/ |
|
static void |
|
pnode_unlink(struct pnode *pn) |
|
{ |
|
|
|
if (NULL != pn->parent) |
|
TAILQ_REMOVE(&pn->parent->childq, pn, child); |
|
pnode_free(pn); |
|
} |
|
|
|
/* |
|
* Unlink all children of a node and pnode_free() them. |
|
*/ |
|
static void |
|
pnode_unlinksub(struct pnode *pn) |
|
{ |
|
|
|
while ( ! TAILQ_EMPTY(&pn->childq)) |
|
pnode_unlink(TAILQ_FIRST(&pn->childq)); |
|
} |
|
|
|
/* |
|
* Retrieve an enumeration attribute from a node. |
|
* Return ATTRVAL__MAX if the node has no such attribute. |
|
*/ |
|
enum attrval |
|
pnode_getattr(struct pnode *pn, enum attrkey key) |
|
{ |
|
struct pattr *ap; |
|
|
|
TAILQ_FOREACH(ap, &pn->attrq, child) |
|
if (ap->key == key) |
|
return ap->val; |
|
return(ATTRVAL__MAX); |
|
} |
|
|
|
/* |
|
* Retrieve an attribute string from a node. |
|
* Return defval if the node has no such attribute. |
|
*/ |
|
const char * |
|
pnode_getattr_raw(struct pnode *pn, enum attrkey key, const char *defval) |
|
{ |
|
struct pattr *ap; |
|
|
|
TAILQ_FOREACH(ap, &pn->attrq, child) |
|
if (ap->key == key) |
|
return ATTRVAL__MAX == ap->val ? ap->rawval : |
|
attrvals[ap->val]; |
|
return(defval); |
|
} |
|
|
|
/* |
|
* Reset the lookaside buffer. |
|
*/ |
|
static void |
|
bufclear(struct parse *p) |
|
{ |
|
|
|
p->b[p->bsz = 0] = '\0'; |
|
} |
|
|
|
/* |
|
* Append NODE_TEXT contents to the current buffer, reallocating its |
|
* size if necessary. |
|
* The buffer is ALWAYS NUL-terminated. |
|
*/ |
|
static void |
|
bufappend(struct parse *p, struct pnode *pn) |
|
{ |
|
|
|
assert(NODE_TEXT == pn->node); |
|
if (p->bsz + pn->bsz + 1 > p->mbsz) { |
|
p->mbsz = p->bsz + pn->bsz + 1; |
|
if (NULL == (p->b = realloc(p->b, p->mbsz))) { |
|
perror(NULL); |
|
exit(EXIT_FAILURE); |
|
} |
} |
} |
putchar(last = *cp); |
memcpy(p->b + p->bsz, pn->b, pn->bsz); |
if (last == '\\') |
p->bsz += pn->bsz; |
|
p->b[p->bsz] = '\0'; |
|
} |
|
|
|
/* |
|
* Recursively append all NODE_TEXT nodes to the buffer. |
|
* This descends into non-text nodes, but doesn't do anything beyond |
|
* them. |
|
* In other words, this is a recursive text grok. |
|
*/ |
|
static void |
|
bufappend_r(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
|
|
if (NODE_TEXT == pn->node) |
|
bufappend(p, pn); |
|
TAILQ_FOREACH(pp, &pn->childq, child) |
|
bufappend_r(p, pp); |
|
} |
|
|
|
/* |
|
* Recursively search and return the first instance of "node". |
|
*/ |
|
static struct pnode * |
|
pnode_findfirst(struct pnode *pn, enum nodeid node) |
|
{ |
|
struct pnode *pp, *res; |
|
|
|
res = NULL; |
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
|
res = pp->node == node ? pp : |
|
pnode_findfirst(pp, node); |
|
if (NULL != res) |
|
break; |
|
} |
|
|
|
return(res); |
|
} |
|
|
|
#define MACROLINE_NORM 0 |
|
#define MACROLINE_UPPER 1 |
|
#define MACROLINE_NOWS 2 |
|
/* |
|
* Recursively print text presumably on a macro line. |
|
* Convert all whitespace to regular spaces. |
|
*/ |
|
static void |
|
pnode_printmacrolinetext(struct parse *p, struct pnode *pn, int fl) |
|
{ |
|
char *cp; |
|
|
|
if (0 == p->newln && ! (MACROLINE_NOWS & fl)) |
|
putchar(' '); |
|
|
|
bufclear(p); |
|
bufappend_r(p, pn); |
|
|
|
/* Convert all space to spaces. */ |
|
for (cp = p->b; '\0' != *cp; cp++) |
|
if (isspace((unsigned char)*cp)) |
|
*cp = ' '; |
|
|
|
for (cp = p->b; isspace((unsigned char)*cp); cp++) |
|
/* Spin past whitespace (XXX: necessary?) */ ; |
|
for ( ; '\0' != *cp; cp++) { |
|
/* Escape us if we look like a macro. */ |
|
if ((cp == p->b || ' ' == *(cp - 1)) && |
|
isupper((unsigned char)*cp) && |
|
'\0' != *(cp + 1) && |
|
islower((unsigned char)*(cp + 1)) && |
|
('\0' == *(cp + 2) || |
|
' ' == *(cp + 2) || |
|
(islower((unsigned char)*(cp + 2)) && |
|
('\0' == *(cp + 3) || |
|
' ' == *(cp + 3))))) |
|
fputs("\\&", stdout); |
|
if (MACROLINE_UPPER & fl) |
|
putchar(toupper((unsigned char)*cp)); |
|
else |
|
putchar(*cp); |
|
/* If we're a character escape, escape us. */ |
|
if ('\\' == *cp) |
|
putchar('e'); |
putchar('e'); |
} |
} |
} |
} |
|
|
static void |
static void |
pnode_printmacrolinepart(struct parse *p, struct pnode *pn) |
pnode_printpara(struct format *p, struct pnode *pn) |
{ |
{ |
|
|
pnode_printmacrolinetext(p, pn, 0); |
|
} |
|
|
|
/* |
|
* Just pnode_printmacrolinepart() but with a newline. |
|
* If no text, just the newline. |
|
*/ |
|
static void |
|
pnode_printmacroline(struct parse *p, struct pnode *pn) |
|
{ |
|
|
|
assert(0 == p->newln); |
|
pnode_printmacrolinetext(p, pn, 0); |
|
putchar('\n'); |
|
p->newln = 1; |
|
} |
|
|
|
static void |
|
pnode_printmopen(struct parse *p) |
|
{ |
|
if (p->newln) { |
|
putchar('.'); |
|
p->newln = 0; |
|
} else |
|
putchar(' '); |
|
} |
|
|
|
static void |
|
pnode_printmclose(struct parse *p, int sv) |
|
{ |
|
|
|
if (sv && ! p->newln) { |
|
putchar('\n'); |
|
p->newln = 1; |
|
} |
|
} |
|
|
|
/* |
|
* Like pnode_printmclose() except we look to the next node, and, if |
|
* found, see if it starts with punctuation. |
|
* If it does, then we print that punctuation before the newline. |
|
*/ |
|
static void |
|
pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv) |
|
{ |
|
/* We wouldn't have done anything anyway. */ |
|
if ( ! (sv && ! p->newln)) |
|
return; |
|
|
|
/* No next node or it's not text. */ |
|
if (NULL == (pn = TAILQ_NEXT(pn, child))) { |
|
pnode_printmclose(p, sv); |
|
return; |
|
} else if (NODE_TEXT != pn->node) { |
|
pnode_printmclose(p, sv); |
|
return; |
|
} |
|
|
|
/* Only do this for the comma/period. */ |
|
if (pn->bsz > 0 && |
|
(',' == pn->b[0] || '.' == pn->b[0]) && |
|
(1 == pn->bsz || isspace((unsigned char)pn->b[1]))) { |
|
putchar(' '); |
|
putchar(pn->b[0]); |
|
pn->b++; |
|
pn->bsz--; |
|
} |
|
|
|
putchar('\n'); |
|
p->newln = 1; |
|
} |
|
|
|
static void |
|
pnode_printpara(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
struct pnode *pp; |
|
|
assert(p->newln); |
|
if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL && |
if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL && |
(pp = pn->parent) == NULL) |
(pp = pn->parent) == NULL) |
return; |
return; |
Line 736 pnode_printpara(struct parse *p, struct pnode *pn) |
|
Line 119 pnode_printpara(struct parse *p, struct pnode *pn) |
|
default: |
default: |
break; |
break; |
} |
} |
puts(".Pp"); |
macro_line(p, "Pp"); |
} |
} |
|
|
/* |
/* |
* If the SYNOPSIS macro has a superfluous title, kill it. |
* If the SYNOPSIS macro has a superfluous title, kill it. |
*/ |
*/ |
static void |
static void |
pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn) |
pnode_printrefsynopsisdiv(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp, *pq; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) |
if (NODE_TITLE == pp->node) { |
if (pp->node == NODE_TITLE) |
pnode_unlink(pp); |
pnode_unlink(pp); |
return; |
|
} |
macro_line(p, "Sh SYNOPSIS"); |
} |
} |
|
|
/* |
/* |
* Start a hopefully-named `Sh' section. |
* Start a hopefully-named `Sh' section. |
*/ |
*/ |
static void |
static void |
pnode_printrefsect(struct parse *p, struct pnode *pn) |
pnode_printrefsect(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
const char *title; |
const char *title; |
int flags, level; |
int flags, level; |
|
|
if (NULL == pn->parent) |
if (pn->parent == NULL) |
return; |
return; |
|
|
level = ++p->level; |
level = ++p->level; |
flags = 1 == level ? MACROLINE_UPPER : 0; |
flags = ARG_SPACE; |
if (3 > level) { |
if (level == 1) |
|
flags |= ARG_UPPER; |
|
if (level < 3) { |
switch (pn->node) { |
switch (pn->node) { |
case NODE_CAUTION: |
case NODE_CAUTION: |
case NODE_NOTE: |
case NODE_NOTE: |
Line 783 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
Line 168 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
} |
} |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TITLE == pp->node) |
if (pp->node == NODE_TITLE) |
break; |
break; |
|
|
if (NULL == pp) { |
if (pp == NULL) { |
switch (pn->node) { |
switch (pn->node) { |
case NODE_PREFACE: |
case NODE_PREFACE: |
title = "Preface"; |
title = "Preface"; |
Line 811 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
Line 196 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
|
|
switch (level) { |
switch (level) { |
case 1: |
case 1: |
fputs(".Sh", stdout); |
macro_close(p); |
|
macro_open(p, "Sh"); |
break; |
break; |
case 2: |
case 2: |
fputs(".Ss", stdout); |
macro_close(p); |
|
macro_open(p, "Ss"); |
break; |
break; |
default: |
default: |
pnode_printpara(p, pn); |
pnode_printpara(p, pn); |
fputs(".Sy", stdout); |
macro_open(p, "Sy"); |
break; |
break; |
} |
} |
|
|
if (NULL != pp) { |
if (pp != NULL) { |
p->newln = 0; |
macro_addnode(p, pp, flags); |
pnode_printmacrolinetext(p, pp, flags); |
|
pnode_printmclose(p, 1); |
|
pnode_unlink(pp); |
pnode_unlink(pp); |
} else |
} else |
printf(" %s\n", title); |
macro_addarg(p, title, ARG_SPACE | ARG_QUOTED); |
|
macro_close(p); |
} |
} |
|
|
/* |
/* |
* Start a reference, extracting the title and volume. |
* Start a reference, extracting the title and volume. |
*/ |
*/ |
static void |
static void |
pnode_printciterefentry(struct parse *p, struct pnode *pn) |
pnode_printciterefentry(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *title, *manvol; |
struct pnode *pp, *title, *manvol; |
|
|
title = manvol = NULL; |
title = manvol = NULL; |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) { |
if (NODE_MANVOLNUM == pp->node) |
if (pp->node == NODE_MANVOLNUM) |
manvol = pp; |
manvol = pp; |
else if (NODE_REFENTRYTITLE == pp->node) |
else if (pp->node == NODE_REFENTRYTITLE) |
title = pp; |
title = pp; |
|
} |
if (NULL != title) { |
macro_open(p, "Xr"); |
pnode_printmacrolinepart(p, title); |
if (title == NULL) |
} else |
macro_addarg(p, "unknown", ARG_SPACE); |
fputs(" unknown ", stdout); |
else |
|
macro_addnode(p, title, ARG_SPACE | ARG_SINGLE); |
if (NULL == manvol) { |
if (manvol == NULL) |
puts(" 1"); |
macro_addarg(p, "1", ARG_SPACE); |
p->newln = 1; |
else |
} else |
macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); |
pnode_printmacrolinepart(p, manvol); |
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printrefmeta(struct parse *p, struct pnode *pn) |
pnode_printrefmeta(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *title, *manvol; |
struct pnode *pp, *title, *manvol; |
|
|
title = manvol = NULL; |
title = manvol = NULL; |
assert(p->newln); |
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (pp->node == NODE_MANVOLNUM) |
if (NODE_MANVOLNUM == pp->node) |
|
manvol = pp; |
manvol = pp; |
else if (NODE_REFENTRYTITLE == pp->node) |
else if (pp->node == NODE_REFENTRYTITLE) |
title = pp; |
title = pp; |
|
} |
fputs(".Dt", stdout); |
macro_close(p); |
p->newln = 0; |
macro_open(p, "Dt"); |
|
if (title == NULL) |
if (NULL != title) |
macro_addarg(p, "UNKNOWN", ARG_SPACE); |
pnode_printmacrolinetext(p, title, MACROLINE_UPPER); |
|
else |
else |
fputs(" UNKNOWN ", stdout); |
macro_addnode(p, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
|
if (manvol == NULL) |
if (NULL == manvol) { |
macro_addarg(p, "1", ARG_SPACE); |
puts(" 1"); |
else |
p->newln = 1; |
macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); |
} else |
macro_close(p); |
pnode_printmacroline(p, manvol); |
pnode_unlink(pn); |
} |
} |
|
|
static void |
static void |
pnode_printfuncdef(struct parse *p, struct pnode *pn) |
pnode_printfuncdef(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *ftype, *func; |
struct pnode *nc; |
|
|
assert(p->newln); |
nc = TAILQ_FIRST(&n->childq); |
ftype = func = NULL; |
if (nc != NULL && nc->node == NODE_TEXT) { |
TAILQ_FOREACH(pp, &pn->childq, child) |
macro_argline(f, "Ft", nc->b); |
if (NODE_TEXT == pp->node) |
pnode_unlink(nc); |
ftype = pp; |
|
else if (NODE_FUNCTION == pp->node) |
|
func = pp; |
|
|
|
if (NULL != ftype) { |
|
fputs(".Ft", stdout); |
|
p->newln = 0; |
|
pnode_printmacroline(p, ftype); |
|
} |
} |
|
macro_nodeline(f, "Fo", n, ARG_SINGLE); |
if (NULL != func) { |
pnode_unlinksub(n); |
fputs(".Fo", stdout); |
|
p->newln = 0; |
|
pnode_printmacroline(p, func); |
|
} else { |
|
puts(".Fo UNKNOWN"); |
|
p->newln = 1; |
|
} |
|
} |
} |
|
|
static void |
|
pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp, *ptype, *param; |
|
|
|
assert(p->newln); |
|
ptype = param = NULL; |
|
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_TEXT == pp->node) |
|
ptype = pp; |
|
else if (NODE_PARAMETER == pp->node) |
|
param = pp; |
|
|
|
fputs(".Fa \"", stdout); |
|
p->newln = 0; |
|
if (NULL != ptype) { |
|
pnode_printmacrolinetext(p, ptype, MACROLINE_NOWS); |
|
putchar(' '); |
|
} |
|
|
|
if (NULL != param) |
|
pnode_printmacrolinepart(p, param); |
|
|
|
puts("\""); |
|
p->newln = 1; |
|
} |
|
|
|
/* |
/* |
* The <mml:mfenced> node is a little peculiar. |
* The <mml:mfenced> node is a little peculiar. |
* First, it can have arbitrary open and closing tokens, which default |
* First, it can have arbitrary open and closing tokens, which default |
Line 949 pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
Line 291 pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
* Second, >1 arguments are separated by commas. |
* Second, >1 arguments are separated by commas. |
*/ |
*/ |
static void |
static void |
pnode_printmathfenced(struct parse *p, struct pnode *pn) |
pnode_printmathfenced(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
Line 958 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
Line 300 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
pp = TAILQ_FIRST(&pn->childq); |
pp = TAILQ_FIRST(&pn->childq); |
pnode_print(p, pp); |
pnode_print(p, pp); |
|
|
while (NULL != (pp = TAILQ_NEXT(pp, child))) { |
while ((pp = TAILQ_NEXT(pp, child)) != NULL) { |
putchar(','); |
putchar(','); |
pnode_print(p, pp); |
pnode_print(p, pp); |
} |
} |
|
|
printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")")); |
printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")")); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
/* |
/* |
Line 973 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
Line 315 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
* particular eqn(7) word. |
* particular eqn(7) word. |
*/ |
*/ |
static void |
static void |
pnode_printmath(struct parse *p, struct pnode *pn) |
pnode_printmath(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
Line 996 pnode_printmath(struct parse *p, struct pnode *pn) |
|
Line 338 pnode_printmath(struct parse *p, struct pnode *pn) |
|
|
|
pp = TAILQ_NEXT(pp, child); |
pp = TAILQ_NEXT(pp, child); |
pnode_print(p, pp); |
pnode_print(p, pp); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printfuncprototype(struct parse *p, struct pnode *pn) |
pnode_printfuncprototype(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *fdef; |
struct pnode *pp, *fdef; |
|
|
assert(p->newln); |
|
TAILQ_FOREACH(fdef, &pn->childq, child) |
TAILQ_FOREACH(fdef, &pn->childq, child) |
if (NODE_FUNCDEF == fdef->node) |
if (fdef->node == NODE_FUNCDEF) |
break; |
break; |
|
|
if (NULL != fdef) |
if (fdef != NULL) { |
pnode_printfuncdef(p, fdef); |
pnode_printfuncdef(p, fdef); |
else |
pnode_unlink(fdef); |
puts(".Fo UNKNOWN"); |
} else |
|
macro_line(p, "Fo UNKNOWN"); |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_PARAMDEF == pp->node) |
macro_nodeline(p, "Fa", pp, ARG_SINGLE); |
pnode_printparamdef(p, pp); |
|
|
|
puts(".Fc"); |
macro_line(p, "Fc"); |
p->newln = 1; |
pnode_unlinksub(pn); |
} |
} |
|
|
/* |
/* |
Line 1029 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 371 pnode_printfuncprototype(struct parse *p, struct pnode |
|
* ellipsis following an argument) and optionality. |
* ellipsis following an argument) and optionality. |
*/ |
*/ |
static void |
static void |
pnode_printarg(struct parse *p, struct pnode *pn) |
pnode_printarg(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
struct pattr *ap; |
struct pattr *ap; |
Line 1037 pnode_printarg(struct parse *p, struct pnode *pn) |
|
Line 379 pnode_printarg(struct parse *p, struct pnode *pn) |
|
|
|
isop = 1; |
isop = 1; |
isrep = 0; |
isrep = 0; |
TAILQ_FOREACH(ap, &pn->attrq, child) |
TAILQ_FOREACH(ap, &pn->attrq, child) { |
if (ATTRKEY_CHOICE == ap->key && |
if (ap->key == ATTRKEY_CHOICE && |
(ATTRVAL_PLAIN == ap->val || |
(ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ)) |
ATTRVAL_REQ == ap->val)) |
|
isop = 0; |
isop = 0; |
else if (ATTRKEY_REP == ap->key && |
else if (ap->key == ATTRKEY_REP && ap->val == ATTRVAL_REPEAT) |
(ATTRVAL_REPEAT == ap->val)) |
|
isrep = 1; |
isrep = 1; |
|
|
if (isop) { |
|
pnode_printmopen(p); |
|
fputs("Op", stdout); |
|
} |
} |
|
if (isop) |
|
macro_open(p, "Op"); |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
if (NODE_TEXT == pp->node) { |
if (pp->node == NODE_TEXT) |
pnode_printmopen(p); |
macro_open(p, "Ar"); |
fputs("Ar", stdout); |
|
} |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
if (NODE_TEXT == pp->node && isrep) |
if (isrep && pp->node == NODE_TEXT) |
fputs("...", stdout); |
macro_addarg(p, "...", ARG_SPACE); |
} |
} |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printgroup(struct parse *p, struct pnode *pn) |
pnode_printgroup(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *np; |
struct pnode *pp, *np; |
struct pattr *ap; |
struct pattr *ap; |
Line 1071 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 408 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
|
|
isop = 1; |
isop = 1; |
TAILQ_FOREACH(ap, &pn->attrq, child) |
TAILQ_FOREACH(ap, &pn->attrq, child) |
if (ATTRKEY_CHOICE == ap->key && |
if (ap->key == ATTRKEY_CHOICE && |
(ATTRVAL_PLAIN == ap->val || |
(ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ)) { |
ATTRVAL_REQ == ap->val)) { |
|
isop = 0; |
isop = 0; |
break; |
break; |
} |
} |
Line 1083 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 419 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
* This will prevent pnode_print() for putting us on a |
* This will prevent pnode_print() for putting us on a |
* subsequent line. |
* subsequent line. |
*/ |
*/ |
sv = p->newln; |
sv = p->linestate == LINE_NEW; |
pnode_printmopen(p); |
|
if (isop) |
if (isop) |
fputs("Op", stdout); |
macro_open(p, "Op"); |
else if (sv) |
else if (sv) |
fputs("No", stdout); |
macro_open(p, "No"); |
|
|
/* |
/* |
* Keep on printing text separated by the vertical bar as long |
* Keep on printing text separated by the vertical bar as long |
Line 1101 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 436 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
pnode_print(p, pp); |
pnode_print(p, pp); |
np = TAILQ_NEXT(pp, child); |
np = TAILQ_NEXT(pp, child); |
while (NULL != np) { |
while (np != NULL) { |
if (pp->node != np->node) |
if (pp->node != np->node) |
break; |
break; |
fputs(" |", stdout); |
macro_addarg(p, "|", ARG_SPACE); |
pnode_printmacrolinepart(p, np); |
macro_addnode(p, np, ARG_SPACE); |
pp = np; |
pp = np; |
np = TAILQ_NEXT(np, child); |
np = TAILQ_NEXT(np, child); |
} |
} |
} |
} |
|
if (sv) |
|
macro_close(p); |
|
pnode_unlinksub(pn); |
|
} |
|
|
pnode_printmclose(p, sv); |
static void |
|
pnode_printauthor(struct format *f, struct pnode *n) |
|
{ |
|
struct pnode *nc, *ncn; |
|
int have_contrib, have_name; |
|
|
|
/* |
|
* Print <contrib> children up front, before the .An scope, |
|
* and figure out whether we a name of a person. |
|
*/ |
|
|
|
have_contrib = have_name = 0; |
|
TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) { |
|
switch (nc->node) { |
|
case NODE_CONTRIB: |
|
if (have_contrib) |
|
print_text(f, ",", 0); |
|
print_textnode(f, nc); |
|
pnode_unlink(nc); |
|
have_contrib = 1; |
|
break; |
|
case NODE_PERSONNAME: |
|
have_name = 1; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
if (TAILQ_FIRST(&n->childq) == NULL) |
|
return; |
|
|
|
if (have_contrib) |
|
print_text(f, ":", 0); |
|
|
|
/* |
|
* If we have a name, print it in the .An scope and leave |
|
* all other content for child handlers, to print after the |
|
* scope. Otherwise, print everything in the scope. |
|
*/ |
|
|
|
macro_open(f, "An"); |
|
TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) { |
|
if (nc->node == NODE_PERSONNAME || have_name == 0) { |
|
macro_addnode(f, nc, ARG_SPACE); |
|
pnode_unlink(nc); |
|
} |
|
} |
|
|
|
/* |
|
* If there is an email address, |
|
* print it on the same macro line. |
|
*/ |
|
|
|
if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) { |
|
pnode_print(f, nc); |
|
pnode_unlink(nc); |
|
} |
|
|
|
/* |
|
* If there are still unprinted children, end the scope |
|
* with a comma. Otherwise, leave the scope open in case |
|
* a text node follows that starts with closing punctuation. |
|
*/ |
|
|
|
if (TAILQ_FIRST(&n->childq) != NULL) { |
|
macro_addarg(f, ",", ARG_SPACE); |
|
macro_close(f); |
|
} |
} |
} |
|
|
static void |
static void |
pnode_printprologue(struct parse *p, struct pnode *pn) |
pnode_printlink(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
const char *uri, *text; |
|
|
pp = NULL == p->root ? NULL : |
uri = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL); |
pnode_findfirst(p->root, NODE_REFMETA); |
if (uri != NULL) { |
|
if (TAILQ_FIRST(&n->childq) != NULL) { |
|
print_textnode(f, n); |
|
text = ""; |
|
} else { |
|
text = pnode_getattr_raw(n, ATTRKEY_ENDTERM, NULL); |
|
if (text != NULL) |
|
print_text(f, text, ARG_SPACE); |
|
} |
|
if (text != NULL) |
|
macro_open(f, "Pq"); |
|
macro_open(f, "Sx"); |
|
macro_addarg(f, uri, ARG_SPACE); |
|
pnode_unlinksub(n); |
|
return; |
|
} |
|
uri = pnode_getattr_raw(n, ATTRKEY_XLINK_HREF, NULL); |
|
if (uri == NULL) |
|
uri = pnode_getattr_raw(n, ATTRKEY_URL, NULL); |
|
if (uri != NULL) { |
|
macro_open(f, "Lk"); |
|
macro_addarg(f, uri, ARG_SPACE | ARG_SINGLE); |
|
if (TAILQ_FIRST(&n->childq) != NULL) |
|
macro_addnode(f, n, ARG_SPACE | ARG_SINGLE); |
|
pnode_unlinksub(n); |
|
return; |
|
} |
|
} |
|
|
puts(".Dd $Mdocdate" "$"); |
static void |
if (NULL != pp) { |
pnode_printprologue(struct format *p, struct ptree *tree) |
pnode_printrefmeta(p, pp); |
{ |
pnode_unlink(pp); |
struct pnode *refmeta; |
|
|
|
refmeta = tree->root == NULL ? NULL : |
|
pnode_findfirst(tree->root, NODE_REFMETA); |
|
|
|
macro_line(p, "Dd $Mdocdate" "$"); |
|
if (refmeta == NULL) { |
|
macro_open(p, "Dt"); |
|
macro_addarg(p, |
|
pnode_getattr_raw(tree->root, ATTRKEY_ID, "UNKNOWN"), |
|
ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
|
macro_addarg(p, "1", ARG_SPACE); |
|
macro_close(p); |
} else |
} else |
printf(".Dt %s 1\n", |
pnode_printrefmeta(p, refmeta); |
pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN")); |
macro_line(p, "Os"); |
puts(".Os"); |
|
|
|
if (PARSE_EQN & p->flags) { |
if (tree->flags & TREE_EQN) { |
puts(".EQ"); |
macro_line(p, "EQ"); |
puts("delim $$"); |
print_text(p, "delim $$", 0); |
puts(".EN"); |
macro_line(p, "EN"); |
} |
} |
} |
} |
|
|
Line 1143 pnode_printprologue(struct parse *p, struct pnode *pn) |
|
Line 587 pnode_printprologue(struct parse *p, struct pnode *pn) |
|
* we should comma-separate as list headers. |
* we should comma-separate as list headers. |
*/ |
*/ |
static void |
static void |
pnode_printvarlistentry(struct parse *p, struct pnode *pn) |
pnode_printvarlistentry(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
int first = 1; |
int first = 1; |
|
|
assert(p->newln); |
macro_close(p); |
fputs(".It", stdout); |
macro_open(p, "It"); |
p->newln = 0; |
TAILQ_FOREACH(pp, &pn->childq, child) { |
|
if (pp->node != NODE_TERM) |
|
continue; |
|
if ( ! first) |
|
macro_addarg(p, ",", 0); |
|
pnode_print(p, pp); |
|
first = 0; |
|
} |
|
macro_close(p); |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TERM == pp->node) { |
if (pp->node != NODE_TERM) |
if ( ! first) |
|
putchar(','); |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_unlink(pp); |
pnode_unlinksub(pn); |
first = 0; |
} |
} else |
|
break; |
|
|
|
putchar('\n'); |
static void |
p->newln = 1; |
pnode_printtitle(struct format *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp, *pq; |
|
|
|
TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) { |
|
if (pp->node == NODE_TITLE) { |
|
pnode_printpara(p, pp); |
|
pnode_print(p, pp); |
|
pnode_unlink(pp); |
|
} |
|
} |
} |
} |
|
|
static void |
static void |
pnode_printrow(struct parse *p, struct pnode *pn) |
pnode_printrow(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
puts(".Bl -dash -compact"); |
macro_line(p, "Bl -dash -compact"); |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
assert(p->newln); |
macro_line(p, "It"); |
puts(".It"); |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_printmclose(p, 1); |
|
} |
} |
assert(p->newln); |
macro_line(p, "El"); |
puts(".El"); |
pnode_unlink(pn); |
} |
} |
|
|
static void |
static void |
pnode_printtable(struct parse *p, struct pnode *pn) |
pnode_printtgroup1(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc; |
|
|
assert(p->newln); |
macro_line(f, "Bl -bullet -compact"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) { |
if (NODE_TITLE == pp->node) { |
macro_line(f, "It"); |
pnode_printpara(p, pp); |
pnode_print(f, nc); |
pnode_print(p, pp); |
pnode_unlink(nc); |
pnode_unlink(pp); |
|
} |
|
assert(p->newln); |
|
puts(".Bl -ohang"); |
|
while (NULL != (pp = pnode_findfirst(pn, NODE_ROW))) { |
|
puts(".It Table Row"); |
|
pnode_printrow(p, pp); |
|
pnode_printmclose(p, 1); |
|
pnode_unlink(pp); |
|
} |
} |
assert(p->newln); |
macro_line(f, "El"); |
puts(".El"); |
pnode_unlinksub(n); |
} |
} |
|
|
static void |
static void |
pnode_printlist(struct parse *p, struct pnode *pn) |
pnode_printtgroup2(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nr, *ne; |
|
|
assert(p->newln); |
macro_line(f, "Bl -tag -width Ds"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) { |
if (NODE_TITLE == pp->node) { |
if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL) |
pnode_printpara(p, pp); |
break; |
pnode_print(p, pp); |
macro_close(f); |
pnode_unlink(pp); |
macro_open(f, "It"); |
} |
pnode_print(f, ne); |
assert(p->newln); |
macro_close(f); |
|
pnode_unlink(ne); |
|
pnode_print(f, nr); |
|
pnode_unlink(nr); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
if (NODE_ORDEREDLIST == pn->node) |
static void |
puts(".Bl -enum"); |
pnode_printtgroup(struct format *f, struct pnode *n) |
else |
{ |
puts(".Bl -bullet"); |
struct pnode *nc; |
|
|
|
switch (atoi(pnode_getattr_raw(n, ATTRKEY_COLS, "0"))) { |
|
case 1: |
|
pnode_printtgroup1(f, n); |
|
return; |
|
case 2: |
|
pnode_printtgroup2(f, n); |
|
return; |
|
default: |
|
break; |
|
} |
|
|
|
macro_line(f, "Bl -ohang"); |
|
while ((nc = pnode_findfirst(n, NODE_ROW)) != NULL) { |
|
macro_line(f, "It Table Row"); |
|
pnode_printrow(f, nc); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
|
static void |
|
pnode_printlist(struct format *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
|
|
pnode_printtitle(p, pn); |
|
macro_argline(p, "Bl", |
|
pn->node == NODE_ORDEREDLIST ? "-enum" : "-bullet"); |
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
assert(p->newln); |
macro_line(p, "It"); |
puts(".It"); |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_printmclose(p, 1); |
|
} |
} |
assert(p->newln); |
macro_line(p, "El"); |
puts(".El"); |
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printvariablelist(struct parse *p, struct pnode *pn) |
pnode_printvariablelist(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
assert(p->newln); |
pnode_printtitle(p, pn); |
TAILQ_FOREACH(pp, &pn->childq, child) |
macro_line(p, "Bl -tag -width Ds"); |
if (NODE_TITLE == pp->node) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
pnode_printpara(p, pp); |
if (pp->node == NODE_VARLISTENTRY) |
pnode_print(p, pp); |
pnode_printvarlistentry(p, pp); |
pnode_unlink(pp); |
else |
} |
macro_nodeline(p, "It", pp, 0); |
|
} |
assert(p->newln); |
macro_line(p, "El"); |
puts(".Bl -tag -width Ds"); |
pnode_unlinksub(pn); |
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_VARLISTENTRY != pp->node) { |
|
assert(p->newln); |
|
fputs(".It", stdout); |
|
pnode_printmacroline(p, pp); |
|
} else { |
|
assert(p->newln); |
|
pnode_print(p, pp); |
|
} |
|
assert(p->newln); |
|
puts(".El"); |
|
} |
} |
|
|
/* |
/* |
Line 1271 pnode_printvariablelist(struct parse *p, struct pnode |
|
Line 738 pnode_printvariablelist(struct parse *p, struct pnode |
|
* whatever), don't print inline macros. |
* whatever), don't print inline macros. |
*/ |
*/ |
static void |
static void |
pnode_print(struct parse *p, struct pnode *pn) |
pnode_print(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
const char *ccp; |
enum linestate sv; |
char *cp; |
|
int last, sv; |
|
|
|
if (NULL == pn) |
if (pn == NULL) |
return; |
return; |
|
|
sv = p->newln; |
p->spc = pn->spc; |
|
sv = p->linestate; |
|
|
switch (pn->node) { |
switch (pn->node) { |
case NODE_APPLICATION: |
case NODE_APPLICATION: |
pnode_printmopen(p); |
macro_open(p, "Nm"); |
fputs("Nm", stdout); |
|
break; |
break; |
case NODE_ANCHOR: |
|
/* Don't print anything! */ |
|
return; |
|
case NODE_ARG: |
case NODE_ARG: |
pnode_printarg(p, pn); |
pnode_printarg(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_AUTHOR: |
case NODE_AUTHOR: |
pnode_printmopen(p); |
pnode_printauthor(p, pn); |
fputs("An", stdout); |
|
break; |
break; |
case NODE_AUTHORGROUP: |
case NODE_AUTHORGROUP: |
assert(p->newln); |
macro_line(p, "An -split"); |
puts(".An -split"); |
|
break; |
break; |
case NODE_BOOKINFO: |
case NODE_BOOKINFO: |
assert(p->newln); |
macro_line(p, "Sh NAME"); |
puts(".Sh NAME"); |
|
break; |
break; |
case NODE_CITEREFENTRY: |
case NODE_CITEREFENTRY: |
pnode_printmopen(p); |
|
fputs("Xr", stdout); |
|
pnode_printciterefentry(p, pn); |
pnode_printciterefentry(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_CODE: |
case NODE_CITETITLE: |
pnode_printmopen(p); |
macro_open(p, "%T"); |
fputs("Li", stdout); |
|
break; |
break; |
case NODE_COMMAND: |
case NODE_COMMAND: |
pnode_printmopen(p); |
macro_open(p, "Nm"); |
fputs("Nm", stdout); |
|
break; |
break; |
case NODE_CONSTANT: |
case NODE_CONSTANT: |
pnode_printmopen(p); |
macro_open(p, "Dv"); |
fputs("Dv", stdout); |
|
break; |
break; |
case NODE_EDITOR: |
case NODE_EDITOR: |
puts("editor: "); |
print_text(p, "editor:", ARG_SPACE); |
pnode_printmopen(p); |
sv = LINE_TEXT; |
fputs("An", stdout); |
macro_open(p, "An"); |
break; |
break; |
|
case NODE_EMAIL: |
|
macro_open(p, "Aq Mt"); |
|
break; |
case NODE_EMPHASIS: |
case NODE_EMPHASIS: |
case NODE_FIRSTTERM: |
case NODE_FIRSTTERM: |
pnode_printmopen(p); |
macro_open(p, "Em"); |
fputs("Em", stdout); |
|
break; |
break; |
case NODE_ENVAR: |
case NODE_ENVAR: |
pnode_printmopen(p); |
macro_open(p, "Ev"); |
fputs("Ev", stdout); |
|
break; |
break; |
|
case NODE_ERRORNAME: |
|
macro_open(p, "Er"); |
|
break; |
case NODE_FILENAME: |
case NODE_FILENAME: |
pnode_printmopen(p); |
macro_open(p, "Pa"); |
fputs("Pa", stdout); |
|
break; |
break; |
case NODE_FUNCTION: |
case NODE_FUNCTION: |
pnode_printmopen(p); |
macro_open(p, "Fn"); |
fputs("Fn", stdout); |
|
break; |
break; |
case NODE_FUNCPROTOTYPE: |
case NODE_FUNCPROTOTYPE: |
assert(p->newln); |
|
pnode_printfuncprototype(p, pn); |
pnode_printfuncprototype(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_FUNCSYNOPSISINFO: |
case NODE_FUNCSYNOPSISINFO: |
pnode_printmopen(p); |
macro_open(p, "Fd"); |
fputs("Fd", stdout); |
|
break; |
break; |
case NODE_INDEXTERM: |
|
return; |
|
case NODE_INFORMALEQUATION: |
case NODE_INFORMALEQUATION: |
if ( ! p->newln) |
macro_line(p, "EQ"); |
putchar('\n'); |
|
puts(".EQ"); |
|
p->newln = 0; |
|
break; |
break; |
case NODE_INLINEEQUATION: |
case NODE_INLINEEQUATION: |
fputc('$', stdout); |
if (p->linestate == LINE_NEW) |
p->newln = 0; |
p->linestate = LINE_TEXT; |
|
putchar('$'); |
break; |
break; |
case NODE_ITEMIZEDLIST: |
case NODE_ITEMIZEDLIST: |
assert(p->newln); |
|
pnode_printlist(p, pn); |
pnode_printlist(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_GROUP: |
case NODE_GROUP: |
pnode_printgroup(p, pn); |
pnode_printgroup(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
|
case NODE_KEYSYM: |
|
macro_open(p, "Sy"); |
|
break; |
case NODE_LEGALNOTICE: |
case NODE_LEGALNOTICE: |
assert(p->newln); |
macro_line(p, "Sh LEGAL NOTICE"); |
puts(".Sh LEGAL NOTICE"); |
|
break; |
break; |
case NODE_LINK: |
case NODE_LINK: |
ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL); |
pnode_printlink(p, pn); |
if (NULL == ccp) |
break; |
break; |
|
pnode_printmopen(p); |
|
printf("Sx %s\n", ccp); |
|
p->newln = 1; |
|
return; |
|
case NODE_LITERAL: |
case NODE_LITERAL: |
pnode_printmopen(p); |
macro_open(p, "Ql"); |
fputs("Li", stdout); |
|
break; |
break; |
case NODE_LITERALLAYOUT: |
case NODE_LITERALLAYOUT: |
assert(p->newln); |
macro_close(p); |
puts(".Bd -literal"); |
macro_argline(p, "Bd", pnode_getattr(pn, ATTRKEY_CLASS) == |
|
ATTRVAL_MONOSPACED ? "-literal" : "-unfilled"); |
break; |
break; |
case NODE_MML_MFENCED: |
case NODE_MML_MFENCED: |
pnode_printmathfenced(p, pn); |
pnode_printmathfenced(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_MML_MROW: |
case NODE_MML_MROW: |
case NODE_MML_MI: |
case NODE_MML_MI: |
Line 1413 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 853 pnode_print(struct parse *p, struct pnode *pn) |
|
case NODE_MML_MSUB: |
case NODE_MML_MSUB: |
case NODE_MML_MSUP: |
case NODE_MML_MSUP: |
pnode_printmath(p, pn); |
pnode_printmath(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_OPTION: |
case NODE_OPTION: |
pnode_printmopen(p); |
macro_open(p, "Fl"); |
fputs("Fl", stdout); |
|
break; |
break; |
case NODE_ORDEREDLIST: |
case NODE_ORDEREDLIST: |
assert(p->newln); |
|
pnode_printlist(p, pn); |
pnode_printlist(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_PARA: |
case NODE_PARA: |
pnode_printpara(p, pn); |
pnode_printpara(p, pn); |
break; |
break; |
|
case NODE_PARAMDEF: |
case NODE_PARAMETER: |
case NODE_PARAMETER: |
/* Suppress non-text children... */ |
macro_nodeline(p, "Fa", pn, ARG_SINGLE); |
pnode_printmopen(p); |
|
fputs("Fa \"", stdout); |
|
pnode_printmacrolinetext(p, pn, MACROLINE_NOWS); |
|
fputs("\"", stdout); |
|
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case NODE_QUOTE: |
case NODE_QUOTE: |
pnode_printmopen(p); |
macro_open(p, "Qo"); |
fputs("Qo", stdout); |
|
break; |
break; |
case NODE_PROGRAMLISTING: |
case NODE_PROGRAMLISTING: |
case NODE_SCREEN: |
case NODE_SCREEN: |
assert(p->newln); |
case NODE_SYNOPSIS: |
printf(".Bd %s\n", ATTRVAL_MONOSPACED == |
macro_line(p, "Bd -literal"); |
pnode_getattr(pn, ATTRKEY_CLASS) ? |
|
"-literal" : "-unfilled"); |
|
break; |
break; |
case NODE_REFENTRYINFO: |
case NODE_REFENTRYINFO: |
/* Suppress. */ |
/* Suppress. */ |
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case NODE_REFMETA: |
|
abort(); |
|
break; |
|
case NODE_REFNAME: |
case NODE_REFNAME: |
/* Suppress non-text children... */ |
/* Suppress non-text children... */ |
pnode_printmopen(p); |
macro_open(p, "Nm"); |
fputs("Nm", stdout); |
macro_addnode(p, pn, ARG_SPACE | ARG_SINGLE); |
p->newln = 0; |
|
pnode_printmacrolinepart(p, pn); |
|
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case NODE_REFNAMEDIV: |
case NODE_REFNAMEDIV: |
assert(p->newln); |
macro_line(p, "Sh NAME"); |
puts(".Sh NAME"); |
|
break; |
break; |
case NODE_REFPURPOSE: |
case NODE_REFPURPOSE: |
assert(p->newln); |
macro_open(p, "Nd"); |
pnode_printmopen(p); |
|
fputs("Nd", stdout); |
|
break; |
break; |
case NODE_REFSYNOPSISDIV: |
case NODE_REFSYNOPSISDIV: |
assert(p->newln); |
|
pnode_printrefsynopsisdiv(p, pn); |
pnode_printrefsynopsisdiv(p, pn); |
puts(".Sh SYNOPSIS"); |
|
break; |
break; |
case NODE_PREFACE: |
case NODE_PREFACE: |
case NODE_SECTION: |
case NODE_SECTION: |
Line 1481 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 901 pnode_print(struct parse *p, struct pnode *pn) |
|
case NODE_TIP: |
case NODE_TIP: |
case NODE_CAUTION: |
case NODE_CAUTION: |
case NODE_WARNING: |
case NODE_WARNING: |
assert(p->newln); |
|
pnode_printrefsect(p, pn); |
pnode_printrefsect(p, pn); |
break; |
break; |
case NODE_REPLACEABLE: |
case NODE_REPLACEABLE: |
pnode_printmopen(p); |
macro_open(p, "Ar"); |
fputs("Ar", stdout); |
|
break; |
break; |
case NODE_SBR: |
case NODE_SBR: |
assert(p->newln); |
macro_line(p, "br"); |
puts(".br"); |
|
break; |
break; |
case NODE_SGMLTAG: |
case NODE_SGMLTAG: |
pnode_printmopen(p); |
macro_open(p, "Ic"); |
fputs("Li", stdout); |
|
break; |
break; |
case NODE_STRUCTNAME: |
|
pnode_printmopen(p); |
|
fputs("Vt", stdout); |
|
break; |
|
case NODE_TABLE: |
|
case NODE_INFORMALTABLE: |
|
assert(p->newln); |
|
pnode_printtable(p, pn); |
|
pnode_unlinksub(pn); |
|
break; |
|
case NODE_TEXT: |
case NODE_TEXT: |
if (0 == p->newln) |
case NODE_ESCAPE: |
putchar(' '); |
pnode_printtext(p, pn); |
|
|
bufclear(p); |
|
bufappend(p, pn); |
|
|
|
if (0 == p->bsz) { |
|
assert(pn->real != pn->b); |
|
break; |
|
} |
|
|
|
/* |
|
* Output all characters, squeezing out whitespace |
|
* between newlines. |
|
* XXX: all whitespace, including tabs (?). |
|
* Remember to escape control characters and escapes. |
|
*/ |
|
assert(p->bsz); |
|
cp = p->b; |
|
|
|
/* |
|
* There's often a superfluous "-" in its <option> tags |
|
* before the actual flags themselves. |
|
* "Fl" does this for us, so remove it. |
|
*/ |
|
if (NULL != pn->parent && |
|
NODE_OPTION == pn->parent->node && |
|
'-' == *cp) |
|
cp++; |
|
for (last = '\n'; '\0' != *cp; ) { |
|
if ('\n' == last) { |
|
/* Consume all whitespace. */ |
|
if (isspace((unsigned char)*cp)) { |
|
while (isspace((unsigned char)*cp)) |
|
cp++; |
|
continue; |
|
} else if ('\'' == *cp || '.' == *cp) |
|
fputs("\\&", stdout); |
|
} |
|
putchar(last = *cp++); |
|
/* If we're a character escape, escape us. */ |
|
if ('\\' == last) |
|
putchar('e'); |
|
} |
|
p->newln = 0; |
|
break; |
break; |
|
case NODE_TGROUP: |
|
pnode_printtgroup(p, pn); |
|
break; |
case NODE_TITLE: |
case NODE_TITLE: |
if (pn->parent->node == NODE_BOOKINFO) { |
if (pn->parent->node == NODE_BOOKINFO) { |
pnode_printmopen(p); |
macro_open(p, "Nd"); |
fputs("Nd", stdout); |
break; |
} |
} |
|
pnode_printpara(p, pn); |
|
macro_nodeline(p, "Sy", pn, 0); |
|
pnode_unlinksub(pn); |
break; |
break; |
case NODE_TYPE: |
case NODE_TYPE: |
pnode_printmopen(p); |
macro_open(p, "Vt"); |
fputs("Vt", stdout); |
|
break; |
break; |
case NODE_USERINPUT: |
|
pnode_printmopen(p); |
|
fputs("Li", stdout); |
|
break; |
|
case NODE_VARIABLELIST: |
case NODE_VARIABLELIST: |
assert(p->newln); |
|
pnode_printvariablelist(p, pn); |
pnode_printvariablelist(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_VARLISTENTRY: |
|
assert(p->newln); |
|
pnode_printvarlistentry(p, pn); |
|
break; |
|
case NODE_VARNAME: |
case NODE_VARNAME: |
pnode_printmopen(p); |
macro_open(p, "Va"); |
fputs("Va", stdout); |
|
break; |
break; |
default: |
default: |
break; |
break; |
Line 1589 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 946 pnode_print(struct parse *p, struct pnode *pn) |
|
|
|
switch (pn->node) { |
switch (pn->node) { |
case NODE_INFORMALEQUATION: |
case NODE_INFORMALEQUATION: |
if ( ! p->newln) |
macro_line(p, "EN"); |
putchar('\n'); |
|
puts(".EN"); |
|
p->newln = 1; |
|
break; |
break; |
case NODE_INLINEEQUATION: |
case NODE_INLINEEQUATION: |
fputs("$ ", stdout); |
fputs("$ ", stdout); |
p->newln = sv; |
p->linestate = sv; |
break; |
break; |
|
case NODE_MEMBER: |
|
if ((pp = TAILQ_NEXT(pn, child)) != NULL && |
|
pp->node != NODE_MEMBER) |
|
pp = NULL; |
|
switch (p->linestate) { |
|
case LINE_TEXT: |
|
if (pp != NULL) |
|
print_text(p, ",", 0); |
|
break; |
|
case LINE_MACRO: |
|
if (pp != NULL) |
|
macro_addarg(p, ",", ARG_SPACE); |
|
macro_close(p); |
|
break; |
|
case LINE_NEW: |
|
break; |
|
} |
|
break; |
case NODE_MML_MROW: |
case NODE_MML_MROW: |
case NODE_MML_MI: |
case NODE_MML_MI: |
case NODE_MML_MN: |
case NODE_MML_MN: |
Line 1610 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 982 pnode_print(struct parse *p, struct pnode *pn) |
|
case NODE_ARG: |
case NODE_ARG: |
case NODE_AUTHOR: |
case NODE_AUTHOR: |
case NODE_CITEREFENTRY: |
case NODE_CITEREFENTRY: |
case NODE_CODE: |
case NODE_CITETITLE: |
case NODE_COMMAND: |
case NODE_COMMAND: |
case NODE_CONSTANT: |
case NODE_CONSTANT: |
case NODE_EDITOR: |
case NODE_EDITOR: |
|
case NODE_EMAIL: |
case NODE_EMPHASIS: |
case NODE_EMPHASIS: |
case NODE_ENVAR: |
case NODE_ENVAR: |
|
case NODE_ERRORNAME: |
case NODE_FILENAME: |
case NODE_FILENAME: |
case NODE_FIRSTTERM: |
case NODE_FIRSTTERM: |
case NODE_FUNCTION: |
case NODE_FUNCTION: |
case NODE_FUNCSYNOPSISINFO: |
case NODE_FUNCSYNOPSISINFO: |
|
case NODE_KEYSYM: |
|
case NODE_LINK: |
case NODE_LITERAL: |
case NODE_LITERAL: |
case NODE_OPTION: |
case NODE_OPTION: |
case NODE_PARAMETER: |
case NODE_PARAMETER: |
case NODE_REPLACEABLE: |
case NODE_REPLACEABLE: |
case NODE_REFPURPOSE: |
case NODE_REFPURPOSE: |
case NODE_SGMLTAG: |
case NODE_SGMLTAG: |
case NODE_STRUCTNAME: |
|
case NODE_TEXT: |
|
case NODE_TYPE: |
case NODE_TYPE: |
case NODE_USERINPUT: |
|
case NODE_VARNAME: |
case NODE_VARNAME: |
pnode_printmclosepunct(p, pn, sv); |
if (sv != LINE_MACRO && p->linestate == LINE_MACRO && |
|
(pn->parent == NULL || pn->parent->node != NODE_MEMBER)) |
|
macro_closepunct(p, pn); |
break; |
break; |
case NODE_QUOTE: |
case NODE_QUOTE: |
pnode_printmclose(p, sv); |
if (sv == LINE_NEW) |
sv = p->newln; |
macro_close(p); |
pnode_printmopen(p); |
sv = p->linestate; |
fputs("Qc", stdout); |
macro_open(p, "Qc"); |
pnode_printmclose(p, sv); |
if (sv == LINE_NEW) |
|
macro_close(p); |
break; |
break; |
case NODE_REFNAME: |
case NODE_REFNAME: |
/* |
/* |
Line 1646 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1022 pnode_print(struct parse *p, struct pnode *pn) |
|
* <refname> macros in sequence, then print out a |
* <refname> macros in sequence, then print out a |
* trailing comma before the newline. |
* trailing comma before the newline. |
*/ |
*/ |
if (NULL != pn->parent && |
if (pn->parent != NULL && |
NODE_REFNAMEDIV == pn->parent->node && |
pn->parent->node == NODE_REFNAMEDIV && |
NULL != TAILQ_NEXT(pn, child) && |
TAILQ_NEXT(pn, child) != NULL && |
NODE_REFNAME == TAILQ_NEXT(pn, child)->node) |
TAILQ_NEXT(pn, child)->node == NODE_REFNAME) |
fputs(" ,", stdout); |
macro_addarg(p, ",", ARG_SPACE); |
pnode_printmclose(p, sv); |
if (sv == LINE_NEW) |
|
macro_close(p); |
break; |
break; |
case NODE_PREFACE: |
case NODE_PREFACE: |
case NODE_SECTION: |
case NODE_SECTION: |
Line 1664 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1041 pnode_print(struct parse *p, struct pnode *pn) |
|
case NODE_LITERALLAYOUT: |
case NODE_LITERALLAYOUT: |
case NODE_PROGRAMLISTING: |
case NODE_PROGRAMLISTING: |
case NODE_SCREEN: |
case NODE_SCREEN: |
assert(p->newln); |
case NODE_SYNOPSIS: |
puts(".Ed"); |
macro_line(p, "Ed"); |
p->newln = 1; |
|
break; |
break; |
case NODE_TITLE: |
case NODE_TITLE: |
if (pn->parent->node == NODE_BOOKINFO) { |
if (pn->parent->node == NODE_BOOKINFO) |
pnode_printmclose(p, 1); |
macro_line(p, "Sh AUTHORS"); |
puts(".Sh AUTHORS"); |
|
} |
|
break; |
break; |
default: |
default: |
break; |
break; |
} |
} |
} |
} |
|
|
/* |
void |
* Loop around the read buffer until we've drained it of all data. |
ptree_print(struct ptree *tree) |
* Invoke the parser context with each buffer fill. |
|
*/ |
|
static int |
|
readfile(XML_Parser xp, int fd, |
|
char *b, size_t bsz, const char *fn) |
|
{ |
{ |
struct parse p; |
struct format formatter; |
int rc; |
|
ssize_t ssz; |
|
|
|
memset(&p, 0, sizeof(struct parse)); |
formatter.level = 0; |
|
formatter.linestate = LINE_NEW; |
p.b = malloc(p.bsz = p.mbsz = 1024); |
pnode_printprologue(&formatter, tree); |
p.fname = fn; |
pnode_print(&formatter, tree->root); |
p.xml = xp; |
if (formatter.linestate != LINE_NEW) |
|
putchar('\n'); |
XML_SetCharacterDataHandler(xp, xml_char); |
|
XML_SetElementHandler(xp, xml_elem_start, xml_elem_end); |
|
XML_SetUserData(xp, &p); |
|
|
|
while ((ssz = read(fd, b, bsz)) >= 0) { |
|
if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz))) |
|
fprintf(stderr, "%s:%zu:%zu: %s\n", fn, |
|
XML_GetCurrentLineNumber(xp), |
|
XML_GetCurrentColumnNumber(xp), |
|
XML_ErrorString |
|
(XML_GetErrorCode(xp))); |
|
else if ( ! p.stop && ssz > 0) |
|
continue; |
|
/* |
|
* Exit when we've read all or errors have occured |
|
* during the parse sequence. |
|
*/ |
|
p.newln = 1; |
|
pnode_printprologue(&p, p.root); |
|
pnode_print(&p, p.root); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return(0 != rc && ! p.stop); |
|
} |
|
|
|
/* Read error has occured. */ |
|
perror(fn); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return(0); |
|
} |
|
|
|
int |
|
main(int argc, char *argv[]) |
|
{ |
|
XML_Parser xp; |
|
const char *fname; |
|
char *buf; |
|
int fd, rc, ch; |
|
const char *progname; |
|
|
|
progname = strrchr(argv[0], '/'); |
|
if (progname == NULL) |
|
progname = argv[0]; |
|
else |
|
++progname; |
|
|
|
fname = "-"; |
|
xp = NULL; |
|
buf = NULL; |
|
rc = 0; |
|
|
|
while (-1 != (ch = getopt(argc, argv, "W"))) |
|
switch (ch) { |
|
case 'W': |
|
warn = 1; |
|
break; |
|
default: |
|
goto usage; |
|
} |
|
|
|
argc -= optind; |
|
argv += optind; |
|
|
|
if (argc > 1) { |
|
fprintf(stderr, "%s: Too many arguments\n", argv[1]); |
|
goto usage; |
|
} else if (argc > 0) |
|
fname = argv[0]; |
|
|
|
/* Read from stdin or a file. */ |
|
fd = 0 == strcmp(fname, "-") ? |
|
STDIN_FILENO : open(fname, O_RDONLY, 0); |
|
|
|
/* |
|
* Open file for reading. |
|
* Allocate a read buffer. |
|
* Create the parser context. |
|
* Dive directly into the parse. |
|
*/ |
|
if (-1 == fd) |
|
perror(fname); |
|
else if (NULL == (buf = malloc(4096))) |
|
perror(NULL); |
|
else if (NULL == (xp = XML_ParserCreate(NULL))) |
|
perror(NULL); |
|
else if ( ! readfile(xp, fd, buf, 4096, fname)) |
|
rc = 1; |
|
|
|
XML_ParserFree(xp); |
|
free(buf); |
|
if (STDIN_FILENO != fd) |
|
close(fd); |
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
|
|
|
usage: |
|
fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname); |
|
return(EXIT_FAILURE); |
|
} |
} |