version 1.37, 2014/04/30 12:34:44 |
version 1.98, 2019/04/07 17:55:18 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
* 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? */ |
|
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 { |
static void pnode_print(struct format *, struct pnode *); |
const char *name; /* docbook element name */ |
|
unsigned int flags; |
|
#define NODE_IGNTEXT 1 /* ignore all contained text */ |
|
}; |
|
|
|
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", |
|
"id", |
|
"rep" |
|
}; |
|
|
|
static const char *attrvals[ATTRVAL__MAX] = { |
|
"norepeat", |
|
"opt", |
|
"plain", |
|
"repeat", |
|
"req" |
|
}; |
|
|
|
static const struct node nodes[NODE__MAX] = { |
|
{ NULL, 0 }, |
|
{ "acronym", 0 }, |
|
{ "anchor", NODE_IGNTEXT }, |
|
{ "application", 0 }, |
|
{ "arg", 0 }, |
|
{ "caution", NODE_IGNTEXT }, |
|
{ "citerefentry", NODE_IGNTEXT }, |
|
{ "cmdsynopsis", NODE_IGNTEXT }, |
|
{ "code", 0 }, |
|
{ "command", 0 }, |
|
{ "constant", 0 }, |
|
{ "copyright", NODE_IGNTEXT }, |
|
{ "date", 0 }, |
|
{ "emphasis", 0 }, |
|
{ "entry", 0 }, |
|
{ "envar", 0 }, |
|
{ "filename", 0 }, |
|
{ "funcdef", 0 }, |
|
{ "funcprototype", NODE_IGNTEXT }, |
|
{ "funcsynopsis", NODE_IGNTEXT }, |
|
{ "funcsynopsisinfo", 0 }, |
|
{ "function", 0 }, |
|
{ "group", NODE_IGNTEXT }, |
|
{ "holder", NODE_IGNTEXT }, |
|
{ "info", NODE_IGNTEXT }, |
|
{ "informaltable", NODE_IGNTEXT }, |
|
{ "itemizedlist", NODE_IGNTEXT }, |
|
{ "link", 0 }, |
|
{ "listitem", NODE_IGNTEXT }, |
|
{ "literal", 0 }, |
|
{ "manvolnum", 0 }, |
|
{ "note", NODE_IGNTEXT }, |
|
{ "option", 0 }, |
|
{ "orderedlist", NODE_IGNTEXT }, |
|
{ "para", 0 }, |
|
{ "paramdef", 0 }, |
|
{ "parameter", 0 }, |
|
{ "programlisting", 0 }, |
|
{ "prompt", 0 }, |
|
{ "quote", 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 }, |
|
{ "refsect3", NODE_IGNTEXT }, |
|
{ "refsection", NODE_IGNTEXT }, |
|
{ "refsynopsisdiv", NODE_IGNTEXT }, |
|
{ "replaceable", 0 }, |
|
{ "row", NODE_IGNTEXT }, |
|
{ "sbr", NODE_IGNTEXT }, |
|
{ "screen", NODE_IGNTEXT }, |
|
{ "sgmltag", 0 }, |
|
{ "structname", 0 }, |
|
{ "synopsis", 0 }, |
|
{ "table", NODE_IGNTEXT }, |
|
{ "tbody", NODE_IGNTEXT }, |
|
{ "term", 0 }, |
|
{ NULL, 0 }, |
|
{ "tfoot", NODE_IGNTEXT }, |
|
{ "tgroup", NODE_IGNTEXT }, |
|
{ "thead", NODE_IGNTEXT }, |
|
{ "tip", NODE_IGNTEXT }, |
|
{ "title", 0 }, |
|
{ "trademark", 0 }, |
|
{ "ulink", 0 }, |
|
{ "userinput", 0 }, |
|
{ "variablelist", NODE_IGNTEXT }, |
|
{ "varlistentry", NODE_IGNTEXT }, |
|
{ "varname", 0 }, |
|
{ "warning", NODE_IGNTEXT }, |
|
{ "wordasword", 0 }, |
|
{ "year", NODE_IGNTEXT }, |
|
}; |
|
|
|
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; |
|
} |
|
|
/* 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? |
* 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((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); |
|
} |
|
|
|
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((int)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; |
|
enum nodeid 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; |
|
} |
} |
|
|
|
/* |
|
* Remove the prefix '-' from <option> elements |
|
* because the arguments of .Fl macros do not need it. |
|
*/ |
|
|
for (node = 0; node < NODE__MAX; node++) |
cp = n->b; |
if (NULL == nodes[node].name) |
if (n->parent != NULL && n->parent->node == NODE_OPTION && *cp == '-') |
continue; |
cp++; |
else if (0 == strcmp(nodes[node].name, name)) |
|
break; |
|
|
|
if (NODE__MAX == node && NODE_ROOT == ps->node) { |
|
return; |
|
} else if (NODE__MAX == node) { |
|
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; |
|
} else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) { |
|
return; |
|
} else if ( ! isparent(node, ps->node)) { |
|
fprintf(stderr, "%s:%zu:%zu: bad parent \"%s\" " |
|
"of node \"%s\"\n", |
|
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml), |
|
NULL == nodes[ps->node].name ? |
|
"(none)" : nodes[ps->node].name, |
|
NULL == nodes[node].name ? |
|
"(none)" : nodes[node].name); |
|
ps->stop = 1; |
|
return; |
|
} |
|
|
|
if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { |
|
perror(NULL); |
|
exit(EXIT_FAILURE); |
|
} |
|
|
|
dat->node = ps->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++) |
for (; *cp != '\0'; cp++) { |
if (0 == strcmp(*att, attrkeys[key])) |
if (last == '\n') { |
break; |
if (isspace((unsigned char)*cp)) |
if (ATTRKEY__MAX == key) { |
continue; |
fprintf(stderr, "%s:%zu:%zu: warning: " |
if (*cp == '\'' || *cp == '.') |
"unknown attribute \"%s\"\n", |
fputs("\\&", stdout); |
ps->fname, |
|
XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml), |
|
*att); |
|
continue; |
|
} else if ( ! isattrkey(node, key)) { |
|
fprintf(stderr, "%s:%zu:%zu: warning: " |
|
"bad attribute \"%s\"\n", |
|
ps->fname, |
|
XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml), |
|
*att); |
|
continue; |
|
} |
} |
for (val = 0; val < ATTRVAL__MAX; val++) |
putchar(last = *cp); |
if (0 == strcmp(*(att + 1), attrvals[val])) |
if (last == '\\') |
break; |
putchar('e'); |
if (ATTRVAL__MAX != val && ! isattrval(key, val)) { |
|
fprintf(stderr, "%s:%zu:%zu: warning: " |
|
"bad attribute value \"%s\"\n", |
|
ps->fname, |
|
XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml), |
|
*(att + 1)); |
|
continue; |
|
} |
|
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); |
|
} |
} |
|
|
} |
} |
|
|
/* |
|
* Roll up the parse tree. |
|
* If we're at a text node, roll that one up first. |
|
* If we hit the root, then assign ourselves as the NODE_ROOT. |
|
*/ |
|
static void |
static void |
xml_elem_end(void *arg, const XML_Char *name) |
pnode_printpara(struct format *p, struct pnode *pn) |
{ |
{ |
struct parse *ps = arg; |
struct pnode *pp; |
|
|
/* FIXME: find a better way to ditch other namespaces. */ |
if (pn->parent == NULL) |
if (ps->stop || NODE_ROOT == ps->node) |
|
return; |
return; |
else if (0 == strcmp(name, "xi:include")) |
|
return; |
|
|
|
/* Close out text node, if applicable... */ |
if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL) |
if (NODE_TEXT == ps->node) { |
pp = pn->parent; |
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)) |
switch (pp->node) { |
ps->node = NODE_ROOT; |
case NODE_ENTRY: |
else |
case NODE_LISTITEM: |
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; |
return; |
|
case NODE_PREFACE: |
while (NULL != (pp = TAILQ_FIRST(&pn->childq))) { |
case NODE_SECTION: |
TAILQ_REMOVE(&pn->childq, pp, child); |
if (p->level < 3) |
pnode_free(pp); |
return; |
|
break; |
|
default: |
|
break; |
} |
} |
|
macro_line(p, "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. |
* If the SYNOPSIS macro has a superfluous title, kill it. |
*/ |
*/ |
static void |
static void |
pnode_unlink(struct pnode *pn) |
pnode_printrefsynopsisdiv(struct format *p, struct pnode *pn) |
{ |
{ |
|
struct pnode *pp, *pq; |
|
|
if (NULL != pn->parent) |
TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) |
TAILQ_REMOVE(&pn->parent->childq, pn, child); |
if (pp->node == NODE_TITLE) |
pnode_free(pn); |
pnode_unlink(pp); |
} |
|
|
|
/* |
macro_line(p, "Sh SYNOPSIS"); |
* 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)); |
|
} |
} |
|
|
/* |
/* |
* Reset the lookaside buffer. |
* Start a hopefully-named `Sh' section. |
*/ |
*/ |
static void |
static void |
bufclear(struct parse *p) |
pnode_printrefsect(struct format *p, struct pnode *pn) |
{ |
{ |
|
struct pnode *pp; |
|
const char *title; |
|
int flags, level; |
|
|
p->b[p->bsz = 0] = '\0'; |
if (pn->parent == NULL) |
} |
return; |
|
|
/* |
level = ++p->level; |
* Append NODE_TEXT contents to the current buffer, reallocating its |
flags = ARG_SPACE; |
* size if necessary. |
if (level == 1) |
* The buffer is ALWAYS nil-terminated. |
flags |= ARG_UPPER; |
*/ |
if (level < 3) { |
static void |
switch (pn->node) { |
bufappend(struct parse *p, struct pnode *pn) |
case NODE_CAUTION: |
{ |
case NODE_NOTE: |
|
case NODE_TIP: |
assert(NODE_TEXT == pn->node); |
case NODE_WARNING: |
if (p->bsz + pn->bsz + 1 > p->mbsz) { |
level = 3; |
p->mbsz = p->bsz + pn->bsz + 1; |
break; |
if (NULL == (p->b = realloc(p->b, p->mbsz))) { |
default: |
perror(NULL); |
break; |
exit(EXIT_FAILURE); |
|
} |
} |
} |
} |
memcpy(p->b + p->bsz, pn->b, pn->bsz); |
|
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) |
TAILQ_FOREACH(pp, &pn->childq, child) |
bufappend_r(p, pp); |
if (pp->node == NODE_TITLE) |
} |
break; |
|
|
/* |
if (pp == NULL) { |
* Recursively search and return the first instance of "node". |
switch (pn->node) { |
*/ |
case NODE_PREFACE: |
static struct pnode * |
title = "Preface"; |
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; |
break; |
|
case NODE_CAUTION: |
|
title = "Caution"; |
|
break; |
|
case NODE_NOTE: |
|
title = "Note"; |
|
break; |
|
case NODE_TIP: |
|
title = "Tip"; |
|
break; |
|
case NODE_WARNING: |
|
title = "Warning"; |
|
break; |
|
default: |
|
title = "Unknown"; |
|
break; |
|
} |
} |
} |
|
|
return(res); |
switch (level) { |
} |
case 1: |
|
macro_close(p); |
#define MACROLINE_NORM 0 |
macro_open(p, "Sh"); |
#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((int)*cp)) |
|
*cp = ' '; |
|
|
|
for (cp = p->b; isspace((int)*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((int)*cp) && |
|
'\0' != *(cp + 1) && |
|
islower((int)*(cp + 1)) && |
|
('\0' == *(cp + 2) || |
|
' ' == *(cp + 2) || |
|
(islower((int)*(cp + 2)) && |
|
('\0' == *(cp + 3) || |
|
' ' == *(cp + 3))))) |
|
fputs("\\&", stdout); |
|
if (MACROLINE_UPPER & fl) |
|
putchar(toupper((int)*cp)); |
|
else |
|
putchar((int)*cp); |
|
/* If we're a character escape, escape us. */ |
|
if ('\\' == *cp) |
|
putchar('e'); |
|
} |
|
} |
|
|
|
static void |
|
pnode_printmacrolinepart(struct parse *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((int)pn->b[1]))) { |
|
putchar(' '); |
|
putchar(pn->b[0]); |
|
pn->b++; |
|
pn->bsz--; |
|
} |
|
|
|
putchar('\n'); |
|
p->newln = 1; |
|
} |
|
|
|
/* |
|
* If the SYNOPSIS macro has a superfluous title, kill it. |
|
*/ |
|
static void |
|
pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_TITLE == pp->node) { |
|
pnode_unlink(pp); |
|
return; |
|
} |
|
} |
|
|
|
/* |
|
* Start a hopefully-named `Sh' section. |
|
*/ |
|
static void |
|
pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_TITLE == pp->node) |
|
break; |
|
|
|
switch (pn->node) { |
|
case (NODE_REFSECT1): |
|
fputs(".Sh", stdout); |
|
break; |
break; |
case (NODE_REFSECT2): |
case 2: |
fputs(".Ss", stdout); |
macro_close(p); |
|
macro_open(p, "Ss"); |
break; |
break; |
case (NODE_REFSECT3): |
|
puts(".Pp"); |
|
fputs(".Sy", stdout); |
|
break; |
|
case (NODE_NOTE): |
|
/* FALLTHROUGH */ |
|
case (NODE_REFSECTION): |
|
/* FALLTHROUGH */ |
|
case (NODE_TIP): |
|
/* FALLTHROUGH */ |
|
case (NODE_CAUTION): |
|
/* FALLTHROUGH */ |
|
case (NODE_WARNING): |
|
puts(".Pp"); |
|
if (NULL == pp) |
|
return; |
|
fputs(".Em", stdout); |
|
break; |
|
default: |
default: |
|
pnode_printpara(p, pn); |
|
macro_open(p, "Sy"); |
break; |
break; |
} |
} |
|
|
p->newln = 0; |
if (pp != NULL) { |
|
macro_addnode(p, pp, flags); |
if (NULL != pp) { |
|
pnode_printmacrolinetext(p, pp, |
|
NODE_REFSECT1 == pn->node ? |
|
MACROLINE_UPPER : 0); |
|
pnode_printmclose(p, 1); |
|
pnode_unlink(pp); |
pnode_unlink(pp); |
} else { |
} else |
puts(NODE_REFSECT1 == pn->node ? |
macro_addarg(p, title, ARG_SPACE | ARG_QUOTED); |
"UNKNOWN" : "unknown"); |
macro_close(p); |
p->newln = 1; |
|
} |
|
} |
} |
|
|
/* |
/* |
* 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; |
|
} |
puts(".Dd $Mdocdate" "$"); |
macro_close(p); |
fputs(".Dt", stdout); |
macro_open(p, "Dt"); |
p->newln = 0; |
if (title == NULL) |
|
macro_addarg(p, "UNKNOWN", ARG_SPACE); |
if (NULL != title) |
|
pnode_printmacrolinetext(p, title, MACROLINE_UPPER); |
|
else |
else |
fputs(" UNKNOWN ", stdout); |
macro_addnode(p, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
|
if (manvol == NULL) |
|
macro_addarg(p, "1", ARG_SPACE); |
|
else |
|
macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); |
|
macro_close(p); |
|
pnode_unlink(pn); |
|
} |
|
|
if (NULL == manvol) { |
static void |
puts(" 1"); |
pnode_printfuncdef(struct format *f, struct pnode *n) |
p->newln = 1; |
{ |
} else |
struct pnode *nc; |
pnode_printmacroline(p, manvol); |
|
|
|
puts(".Os"); |
nc = TAILQ_FIRST(&n->childq); |
|
if (nc != NULL && nc->node == NODE_TEXT) { |
|
macro_argline(f, "Ft", nc->b); |
|
pnode_unlink(nc); |
|
} |
|
macro_nodeline(f, "Fo", n, ARG_SINGLE); |
|
pnode_unlinksub(n); |
} |
} |
|
|
|
/* |
|
* The <mml:mfenced> node is a little peculiar. |
|
* First, it can have arbitrary open and closing tokens, which default |
|
* to parentheses. |
|
* Second, >1 arguments are separated by commas. |
|
*/ |
static void |
static void |
pnode_printfuncdef(struct parse *p, struct pnode *pn) |
pnode_printmathfenced(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *ftype, *func; |
struct pnode *pp; |
|
|
assert(p->newln); |
printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "(")); |
ftype = func = NULL; |
|
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_TEXT == pp->node) |
|
ftype = pp; |
|
else if (NODE_FUNCTION == pp->node) |
|
func = pp; |
|
|
|
if (NULL != ftype) { |
pp = TAILQ_FIRST(&pn->childq); |
fputs(".Ft", stdout); |
pnode_print(p, pp); |
p->newln = 0; |
|
pnode_printmacroline(p, ftype); |
|
} |
|
|
|
if (NULL != func) { |
while ((pp = TAILQ_NEXT(pp, child)) != NULL) { |
fputs(".Fo", stdout); |
putchar(','); |
p->newln = 0; |
pnode_print(p, pp); |
pnode_printmacroline(p, func); |
|
} else { |
|
puts(".Fo UNKNOWN"); |
|
p->newln = 1; |
|
} |
} |
|
printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")")); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
|
/* |
|
* These math nodes require special handling because they have infix |
|
* syntax, instead of the usual prefix or prefix. |
|
* So we need to break up the first and second child node with a |
|
* particular eqn(7) word. |
|
*/ |
static void |
static void |
pnode_printparamdef(struct parse *p, struct pnode *pn) |
pnode_printmath(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *ptype, *param; |
struct pnode *pp; |
|
|
assert(p->newln); |
pp = TAILQ_FIRST(&pn->childq); |
ptype = param = NULL; |
pnode_print(p, pp); |
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_TEXT == pp->node) |
|
ptype = pp; |
|
else if (NODE_PARAMETER == pp->node) |
|
param = pp; |
|
|
|
fputs(".Fa \"", stdout); |
switch (pn->node) { |
p->newln = 0; |
case NODE_MML_MSUP: |
if (NULL != ptype) { |
fputs(" sup ", stdout); |
pnode_printmacrolinetext(p, ptype, MACROLINE_NOWS); |
break; |
putchar(' '); |
case NODE_MML_MFRAC: |
|
fputs(" over ", stdout); |
|
break; |
|
case NODE_MML_MSUB: |
|
fputs(" sub ", stdout); |
|
break; |
|
default: |
|
break; |
} |
} |
|
|
if (NULL != param) |
pp = TAILQ_NEXT(pp, child); |
pnode_printmacrolinepart(p, param); |
pnode_print(p, pp); |
|
pnode_unlinksub(pn); |
puts("\""); |
|
p->newln = 1; |
|
} |
} |
|
|
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); |
} |
} |
|
|
/* |
/* |
* The <arg> element is more complicated than it should be because text |
* The <arg> element is more complicated than it should be because text |
* nodes are treated like ".Ar foo", but non-text nodes need to be |
* nodes are treated like ".Ar foo", but non-text nodes need to be |
* re-sent into the printer (i.e., without the preceding ".Ar"). |
* re-sent into the printer (i.e., without the preceding ".Ar"). |
Line 872 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 373 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 880 pnode_printarg(struct parse *p, struct pnode *pn) |
|
Line 381 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; |
int isop, sv; |
int isop, sv; |
|
|
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; |
} |
} |
|
|
/* |
/* |
* Make sure we're on a macro line. |
* Make sure we're on a macro line. |
* 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) |
macro_open(p, "Op"); |
fputs("Op", stdout); |
|
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 944 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 438 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) |
pnode_printmclose(p, sv); |
macro_close(p); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printprologue(struct parse *p, struct pnode *pn) |
pnode_printauthor(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc, *ncn; |
|
int have_contrib, have_name; |
|
|
pp = NULL == p->root ? NULL : |
/* |
pnode_findfirst(p->root, NODE_REFMETA); |
* Print <contrib> children up front, before the .An scope, |
|
* and figure out whether we a name of a person. |
|
*/ |
|
|
if (NULL != pp) { |
have_contrib = have_name = 0; |
pnode_printrefmeta(p, pp); |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) { |
pnode_unlink(pp); |
switch (nc->node) { |
} else { |
case NODE_CONTRIB: |
puts(".\\\" Supplying bogus prologue..."); |
if (have_contrib) |
puts(".Dd $Mdocdate" "$"); |
print_text(f, ",", 0); |
puts(".Dt UNKNOWN 1"); |
print_textnode(f, nc); |
puts(".Os"); |
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_printvarlistentry(struct parse *p, struct pnode *pn) |
pnode_printlink(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
const char *uri, *text; |
|
|
assert(p->newln); |
uri = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL); |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (uri != NULL) { |
if (NODE_TERM == pp->node) { |
if (TAILQ_FIRST(&n->childq) != NULL) { |
assert(p->newln); |
print_textnode(f, n); |
fputs(".It", stdout); |
text = ""; |
p->newln = 0; |
} else { |
pnode_print(p, pp); |
text = pnode_getattr_raw(n, ATTRKEY_ENDTERM, NULL); |
pnode_unlink(pp); |
if (text != NULL) |
pnode_printmclose(p, 1); |
print_text(f, text, ARG_SPACE); |
return; |
} |
} |
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(".It"); |
static void |
p->newln = 1; |
pnode_printprologue(struct format *p, struct ptree *tree) |
|
{ |
|
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 |
|
pnode_printrefmeta(p, refmeta); |
|
macro_line(p, "Os"); |
|
|
|
if (tree->flags & TREE_EQN) { |
|
macro_line(p, "EQ"); |
|
print_text(p, "delim $$", 0); |
|
macro_line(p, "EN"); |
|
} |
} |
} |
|
|
|
/* |
|
* We can have multiple <term> elements within a <varlistentry>, which |
|
* we should comma-separate as list headers. |
|
*/ |
static void |
static void |
pnode_printrow(struct parse *p, struct pnode *pn) |
pnode_printvarlistentry(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
int first = 1; |
|
|
puts(".Bl -dash -compact"); |
macro_close(p); |
|
macro_open(p, "It"); |
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
assert(p->newln); |
if (pp->node != NODE_TERM) |
puts(".It"); |
continue; |
|
if ( ! first) |
|
macro_addarg(p, ",", 0); |
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_printmclose(p, 1); |
first = 0; |
} |
} |
assert(p->newln); |
macro_close(p); |
puts(".El"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (pp->node != NODE_TERM) |
|
pnode_print(p, pp); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printtable(struct parse *p, struct pnode *pn) |
pnode_printtitle(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp, *pq; |
|
|
assert(p->newln); |
TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) { |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (pp->node == NODE_TITLE) { |
if (NODE_TITLE == pp->node) { |
pnode_printpara(p, pp); |
puts(".Pp"); |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_unlink(pp); |
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); |
|
puts(".El"); |
|
} |
} |
|
|
static void |
static void |
pnode_printlist(struct parse *p, struct pnode *pn) |
pnode_printrow(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
assert(p->newln); |
macro_line(p, "Bl -dash -compact"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) { |
if (NODE_TITLE == pp->node) { |
macro_line(p, "It"); |
puts(".Pp"); |
pnode_print(p, pp); |
pnode_print(p, pp); |
} |
pnode_unlink(pp); |
macro_line(p, "El"); |
} |
pnode_unlink(pn); |
assert(p->newln); |
} |
|
|
if (NODE_ORDEREDLIST == pn->node) |
static void |
puts(".Bl -enum"); |
pnode_printtgroup1(struct format *f, struct pnode *n) |
else |
{ |
puts(".Bl -item"); |
struct pnode *nc; |
|
|
|
macro_line(f, "Bl -bullet -compact"); |
|
while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) { |
|
macro_line(f, "It"); |
|
pnode_print(f, nc); |
|
pnode_unlink(nc); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
|
static void |
|
pnode_printtgroup2(struct format *f, struct pnode *n) |
|
{ |
|
struct pnode *nr, *ne; |
|
|
|
macro_line(f, "Bl -tag -width Ds"); |
|
while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) { |
|
if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL) |
|
break; |
|
macro_close(f); |
|
macro_open(f, "It"); |
|
pnode_print(f, ne); |
|
macro_close(f); |
|
pnode_unlink(ne); |
|
pnode_print(f, nr); |
|
pnode_unlink(nr); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
|
static void |
|
pnode_printtgroup(struct format *f, struct pnode *n) |
|
{ |
|
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) { |
puts(".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 1102 pnode_printvariablelist(struct parse *p, struct pnode |
|
Line 740 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; |
char *cp; |
enum linestate sv; |
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): |
case NODE_ARG: |
/* Don't print anything! */ |
|
return; |
|
case (NODE_ARG): |
|
pnode_printarg(p, pn); |
pnode_printarg(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_CITEREFENTRY): |
case NODE_AUTHOR: |
pnode_printmopen(p); |
pnode_printauthor(p, pn); |
fputs("Xr", stdout); |
break; |
|
case NODE_AUTHORGROUP: |
|
macro_line(p, "An -split"); |
|
break; |
|
case NODE_BLOCKQUOTE: |
|
macro_line(p, "Bd -ragged -offset indent"); |
|
break; |
|
case NODE_BOOKINFO: |
|
macro_line(p, "Sh NAME"); |
|
break; |
|
case NODE_CITEREFENTRY: |
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_EMPHASIS): |
case NODE_EDITOR: |
pnode_printmopen(p); |
print_text(p, "editor:", ARG_SPACE); |
fputs("Em", stdout); |
sv = LINE_TEXT; |
|
macro_open(p, "An"); |
break; |
break; |
case (NODE_ENVAR): |
case NODE_EMAIL: |
pnode_printmopen(p); |
macro_open(p, "Aq Mt"); |
fputs("Ev", stdout); |
|
break; |
break; |
case (NODE_FILENAME): |
case NODE_EMPHASIS: |
pnode_printmopen(p); |
case NODE_FIRSTTERM: |
fputs("Pa", stdout); |
macro_open(p, "Em"); |
break; |
break; |
case (NODE_FUNCTION): |
case NODE_ENVAR: |
pnode_printmopen(p); |
macro_open(p, "Ev"); |
fputs("Fn", stdout); |
|
break; |
break; |
case (NODE_FUNCPROTOTYPE): |
case NODE_ERRORNAME: |
assert(p->newln); |
macro_open(p, "Er"); |
|
break; |
|
case NODE_FILENAME: |
|
macro_open(p, "Pa"); |
|
break; |
|
case NODE_FUNCTION: |
|
macro_open(p, "Fn"); |
|
break; |
|
case NODE_FUNCPROTOTYPE: |
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_ITEMIZEDLIST): |
case NODE_INFORMALEQUATION: |
assert(p->newln); |
macro_line(p, "EQ"); |
|
break; |
|
case NODE_INLINEEQUATION: |
|
if (p->linestate == LINE_NEW) |
|
p->linestate = LINE_TEXT; |
|
putchar('$'); |
|
break; |
|
case NODE_ITEMIZEDLIST: |
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_LITERAL): |
case NODE_KEYSYM: |
pnode_printmopen(p); |
macro_open(p, "Sy"); |
fputs("Li", stdout); |
|
break; |
break; |
case (NODE_OPTION): |
case NODE_LEGALNOTICE: |
pnode_printmopen(p); |
macro_line(p, "Sh LEGAL NOTICE"); |
fputs("Fl", stdout); |
|
break; |
break; |
case (NODE_ORDEREDLIST): |
case NODE_LINK: |
assert(p->newln); |
pnode_printlink(p, pn); |
pnode_printlist(p, pn); |
|
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_PARA): |
case NODE_LITERAL: |
assert(p->newln); |
macro_open(p, "Ql"); |
if (NULL != pn->parent && |
break; |
NODE_LISTITEM == pn->parent->node) |
case NODE_LITERALLAYOUT: |
|
macro_close(p); |
|
macro_argline(p, "Bd", pnode_getattr(pn, ATTRKEY_CLASS) == |
|
ATTRVAL_MONOSPACED ? "-literal" : "-unfilled"); |
|
break; |
|
case NODE_MML_MFENCED: |
|
pnode_printmathfenced(p, pn); |
|
break; |
|
case NODE_MML_MROW: |
|
case NODE_MML_MI: |
|
case NODE_MML_MN: |
|
case NODE_MML_MO: |
|
if (TAILQ_EMPTY(&pn->childq)) |
break; |
break; |
puts(".Pp"); |
fputs(" { ", stdout); |
break; |
break; |
case (NODE_PARAMETER): |
case NODE_MML_MFRAC: |
/* Suppress non-text children... */ |
case NODE_MML_MSUB: |
pnode_printmopen(p); |
case NODE_MML_MSUP: |
fputs("Fa \"", stdout); |
pnode_printmath(p, pn); |
pnode_printmacrolinetext(p, pn, MACROLINE_NOWS); |
break; |
fputs("\"", stdout); |
case NODE_OPTION: |
|
macro_open(p, "Fl"); |
|
break; |
|
case NODE_ORDEREDLIST: |
|
pnode_printlist(p, pn); |
|
break; |
|
case NODE_PARA: |
|
pnode_printpara(p, pn); |
|
break; |
|
case NODE_PARAMDEF: |
|
case NODE_PARAMETER: |
|
macro_nodeline(p, "Fa", pn, ARG_SINGLE); |
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: |
/* FALLTHROUGH */ |
case NODE_SCREEN: |
case (NODE_SCREEN): |
case NODE_SYNOPSIS: |
assert(p->newln); |
macro_line(p, "Bd -literal"); |
puts(".Bd -literal"); |
|
break; |
break; |
case (NODE_REFENTRYINFO): |
case NODE_REFENTRYINFO: |
/* Suppress. */ |
/* Suppress. */ |
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case (NODE_REFMETA): |
case NODE_REFNAME: |
abort(); |
|
break; |
|
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_REFSECT1): |
case NODE_PREFACE: |
/* FALLTHROUGH */ |
case NODE_SECTION: |
case (NODE_REFSECT2): |
case NODE_NOTE: |
/* FALLTHROUGH */ |
case NODE_TIP: |
case (NODE_REFSECT3): |
case NODE_CAUTION: |
/* FALLTHROUGH */ |
case NODE_WARNING: |
case (NODE_REFSECTION): |
|
/* FALLTHROUGH */ |
|
case (NODE_NOTE): |
|
/* FALLTHROUGH */ |
|
case (NODE_TIP): |
|
/* FALLTHROUGH */ |
|
case (NODE_CAUTION): |
|
/* FALLTHROUGH */ |
|
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): |
case NODE_TEXT: |
pnode_printmopen(p); |
case NODE_ESCAPE: |
fputs("Vt", stdout); |
pnode_printtext(p, pn); |
break; |
break; |
case (NODE_TABLE): |
case NODE_TGROUP: |
/* FALLTHROUGH */ |
pnode_printtgroup(p, pn); |
case (NODE_INFORMALTABLE): |
|
assert(p->newln); |
|
pnode_printtable(p, pn); |
|
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_TEXT): |
case NODE_TITLE: |
if (0 == p->newln) |
if (pn->parent != NULL && |
putchar(' '); |
pn->parent->node == NODE_BOOKINFO) { |
|
macro_open(p, "Nd"); |
bufclear(p); |
|
bufappend(p, pn); |
|
|
|
if (0 == p->bsz) { |
|
assert(pn->real != pn->b); |
|
break; |
break; |
} |
} |
|
pnode_printpara(p, pn); |
/* |
macro_nodeline(p, "Sy", pn, 0); |
* Output all characters, squeezing out whitespace |
pnode_unlinksub(pn); |
* 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((int)*cp)) { |
|
while (isspace((int)*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_USERINPUT): |
case NODE_TYPE: |
pnode_printmopen(p); |
macro_open(p, "Vt"); |
fputs("Li", stdout); |
|
break; |
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): |
case NODE_VARNAME: |
assert(p->newln); |
macro_open(p, "Va"); |
pnode_printvarlistentry(p, pn); |
|
break; |
break; |
case (NODE_VARNAME): |
|
pnode_printmopen(p); |
|
fputs("Va", stdout); |
|
break; |
|
default: |
default: |
break; |
break; |
} |
} |
Line 1357 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 951 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
|
|
switch (pn->node) { |
switch (pn->node) { |
case (NODE_APPLICATION): |
case NODE_INFORMALEQUATION: |
case (NODE_ARG): |
macro_line(p, "EN"); |
case (NODE_CITEREFENTRY): |
|
case (NODE_CODE): |
|
case (NODE_COMMAND): |
|
case (NODE_CONSTANT): |
|
case (NODE_EMPHASIS): |
|
case (NODE_ENVAR): |
|
case (NODE_FILENAME): |
|
case (NODE_FUNCTION): |
|
case (NODE_FUNCSYNOPSISINFO): |
|
case (NODE_LITERAL): |
|
case (NODE_OPTION): |
|
case (NODE_PARAMETER): |
|
case (NODE_REPLACEABLE): |
|
case (NODE_REFPURPOSE): |
|
case (NODE_SGMLTAG): |
|
case (NODE_STRUCTNAME): |
|
case (NODE_TEXT): |
|
case (NODE_USERINPUT): |
|
case (NODE_VARNAME): |
|
pnode_printmclosepunct(p, pn, sv); |
|
break; |
break; |
case (NODE_QUOTE): |
case NODE_INLINEEQUATION: |
pnode_printmclose(p, sv); |
fputs("$ ", stdout); |
sv = p->newln; |
p->linestate = sv; |
pnode_printmopen(p); |
|
fputs("Qc", stdout); |
|
pnode_printmclose(p, sv); |
|
break; |
break; |
case (NODE_REFNAME): |
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_MI: |
|
case NODE_MML_MN: |
|
case NODE_MML_MO: |
|
if (TAILQ_EMPTY(&pn->childq)) |
|
break; |
|
fputs(" } ", stdout); |
|
break; |
|
case NODE_APPLICATION: |
|
case NODE_ARG: |
|
case NODE_AUTHOR: |
|
case NODE_CITEREFENTRY: |
|
case NODE_CITETITLE: |
|
case NODE_COMMAND: |
|
case NODE_CONSTANT: |
|
case NODE_EDITOR: |
|
case NODE_EMAIL: |
|
case NODE_EMPHASIS: |
|
case NODE_ENVAR: |
|
case NODE_ERRORNAME: |
|
case NODE_FILENAME: |
|
case NODE_FIRSTTERM: |
|
case NODE_FUNCTION: |
|
case NODE_FUNCSYNOPSISINFO: |
|
case NODE_KEYSYM: |
|
case NODE_LINK: |
|
case NODE_LITERAL: |
|
case NODE_OPTION: |
|
case NODE_PARAMETER: |
|
case NODE_REPLACEABLE: |
|
case NODE_REFPURPOSE: |
|
case NODE_SGMLTAG: |
|
case NODE_TYPE: |
|
case NODE_VARNAME: |
|
if (sv != LINE_MACRO && p->linestate == LINE_MACRO && |
|
(pn->parent == NULL || pn->parent->node != NODE_MEMBER)) |
|
macro_closepunct(p, pn); |
|
break; |
|
case NODE_QUOTE: |
|
if (sv == LINE_NEW) |
|
macro_close(p); |
|
sv = p->linestate; |
|
macro_open(p, "Qc"); |
|
if (sv == LINE_NEW) |
|
macro_close(p); |
|
break; |
|
case NODE_REFNAME: |
/* |
/* |
* If we're in the NAME macro and we have multiple |
* If we're in the NAME macro and we have multiple |
* <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_PROGRAMLISTING): |
case NODE_PREFACE: |
/* FALLTHROUGH */ |
case NODE_SECTION: |
case (NODE_SCREEN): |
case NODE_NOTE: |
assert(p->newln); |
case NODE_TIP: |
puts(".Ed"); |
case NODE_CAUTION: |
p->newln = 1; |
case NODE_WARNING: |
|
p->level--; |
break; |
break; |
|
case NODE_BLOCKQUOTE: |
|
case NODE_LITERALLAYOUT: |
|
case NODE_PROGRAMLISTING: |
|
case NODE_SCREEN: |
|
case NODE_SYNOPSIS: |
|
macro_line(p, "Ed"); |
|
break; |
|
case NODE_TITLE: |
|
if (pn->parent != NULL && |
|
pn->parent->node == NODE_BOOKINFO) |
|
macro_line(p, "Sh AUTHORS"); |
|
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; |
|
|
|
fname = "-"; |
|
xp = NULL; |
|
buf = NULL; |
|
rc = 0; |
|
|
|
if (-1 != getopt(argc, argv, "")) |
|
return(EXIT_FAILURE); |
|
|
|
argc -= optind; |
|
argv += optind; |
|
|
|
if (argc > 1) |
|
return(EXIT_FAILURE); |
|
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); |
|
} |
} |