version 1.70, 2019/03/24 21:00:11 |
version 1.74, 2019/03/26 18:32:07 |
|
|
* 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 "format.h" |
|
|
|
/* |
|
* The implementation of the mdoc(7) formatter. |
|
*/ |
|
|
enum linestate { |
enum linestate { |
LINE_NEW = 0, |
LINE_NEW = 0, |
LINE_TEXT, |
LINE_TEXT, |
LINE_MACRO |
LINE_MACRO |
}; |
}; |
|
|
/* |
struct format { |
* Global parse state. |
int level; /* Header level, starting at 1. */ |
* 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 */ |
|
enum linestate linestate; |
enum linestate linestate; |
}; |
}; |
|
|
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 }, |
|
{ "email", NODE_EMAIL }, |
|
{ "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 }, |
|
{ "personname", NODE_PERSONNAME }, |
|
{ "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); |
macro_open(struct format *p, const char *name) |
|
|
/* |
|
* Process a stream of characters. |
|
* We store text as nodes in and of themselves. |
|
* If a text node is already open, append to it. |
|
* If it's not open, open one under the current context. |
|
*/ |
|
static void |
|
xml_char(void *arg, const XML_Char *p, int sz) |
|
{ |
{ |
struct parse *ps = arg; |
|
struct pnode *dat; |
|
int i; |
|
|
|
/* Stopped or no tree yet. */ |
|
if (ps->stop || ps->node == NODE_ROOT) |
|
return; |
|
|
|
assert(ps->cur != NULL); |
|
|
|
/* |
|
* Are we in the midst of processing text? |
|
* If we're not processing text right now, then create a text |
|
* node for doing so. |
|
* However, don't do so unless we have some non-whitespace to |
|
* process: strip out all leading whitespace to be sure. |
|
*/ |
|
if (ps->node != NODE_TEXT) { |
|
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 (dat == NULL) { |
|
perror(NULL); |
|
exit(1); |
|
} |
|
|
|
dat->node = ps->node = NODE_TEXT; |
|
dat->parent = ps->cur; |
|
TAILQ_INIT(&dat->childq); |
|
TAILQ_INIT(&dat->attrq); |
|
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
|
ps->cur = dat; |
|
assert(ps->root != NULL); |
|
} |
|
|
|
/* Append to current buffer. */ |
|
assert(sz >= 0); |
|
ps->cur->b = realloc(ps->cur->b, |
|
ps->cur->bsz + (size_t)sz); |
|
if (ps->cur->b == NULL) { |
|
perror(NULL); |
|
exit(1); |
|
} |
|
memcpy(ps->cur->b + ps->cur->bsz, p, sz); |
|
ps->cur->bsz += (size_t)sz; |
|
ps->cur->real = ps->cur->b; |
|
} |
|
|
|
static void |
|
pnode_trim(struct pnode *pn) |
|
{ |
|
assert(pn->node == NODE_TEXT); |
|
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 || strcmp(name, "xi:include") == 0) |
|
return; |
|
|
|
/* Close out text node, if applicable... */ |
|
if (ps->node == NODE_TEXT) { |
|
pnode_trim(ps->cur); |
|
ps->cur = ps->cur->parent; |
|
ps->node = ps->cur->node; |
|
} |
|
|
|
for (node = nodes; node->name != NULL; node++) |
|
if (strcmp(node->name, name) == 0) |
|
break; |
|
|
|
if (node->name == NULL) { |
|
if (ps->node == NODE_ROOT) |
|
return; |
|
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 (ps->node == NODE_ROOT && ps->root != NULL) { |
|
fprintf(stderr, "%s:%zu:%zu: multiple refentries\n", |
|
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml)); |
|
ps->stop = 1; |
|
return; |
|
} |
|
|
|
if (node->node == NODE_INLINEEQUATION) |
|
ps->flags |= PARSE_EQN; |
|
|
|
if ((dat = calloc(1, sizeof(struct pnode))) == NULL) { |
|
perror(NULL); |
|
exit(1); |
|
} |
|
|
|
dat->node = ps->node = node->node; |
|
dat->parent = ps->cur; |
|
TAILQ_INIT(&dat->childq); |
|
TAILQ_INIT(&dat->attrq); |
|
|
|
if (ps->cur != NULL) |
|
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
|
|
|
ps->cur = dat; |
|
if (ps->root == NULL) |
|
ps->root = dat; |
|
|
|
/* |
|
* Process attributes. |
|
*/ |
|
for (att = atts; *att != NULL; att += 2) { |
|
for (key = 0; key < ATTRKEY__MAX; key++) |
|
if (strcmp(*att, attrkeys[key]) == 0) |
|
break; |
|
if (key == ATTRKEY__MAX) { |
|
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 (strcmp(att[1], attrvals[val]) == 0) |
|
break; |
|
pattr = calloc(1, sizeof(struct pattr)); |
|
pattr->key = key; |
|
pattr->val = val; |
|
if (val == ATTRVAL__MAX) |
|
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 |
|
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 || ps->node == NODE_ROOT) |
|
return; |
|
else if (strcmp(name, "xi:include") == 0) |
|
return; |
|
|
|
/* Close out text node, if applicable... */ |
|
if (ps->node == NODE_TEXT) { |
|
pnode_trim(ps->cur); |
|
ps->cur = ps->cur->parent; |
|
ps->node = ps->cur->node; |
|
} |
|
|
|
if ((ps->cur = ps->cur->parent) == NULL) |
|
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 (pn == NULL) |
|
return; |
|
|
|
while ((pp = TAILQ_FIRST(&pn->childq)) != NULL) { |
|
TAILQ_REMOVE(&pn->childq, pp, child); |
|
pnode_free(pp); |
|
} |
|
|
|
while ((ap = TAILQ_FIRST(&pn->attrq)) != NULL) { |
|
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 (pn->parent != NULL) |
|
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 ap->val == ATTRVAL__MAX ? 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(pn->node == NODE_TEXT); |
|
if (p->bsz + pn->bsz + 1 > p->mbsz) { |
|
p->mbsz = p->bsz + pn->bsz + 1; |
|
if ((p->b = realloc(p->b, p->mbsz)) == NULL) { |
|
perror(NULL); |
|
exit(1); |
|
} |
|
} |
|
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 (pn->node == NODE_TEXT) |
|
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 (res != NULL) |
|
break; |
|
} |
|
|
|
return res; |
|
} |
|
|
|
static void |
|
macro_open(struct parse *p, const char *name) |
|
{ |
|
switch (p->linestate) { |
switch (p->linestate) { |
case LINE_TEXT: |
case LINE_TEXT: |
putchar('\n'); |
putchar('\n'); |
Line 612 macro_open(struct parse *p, const char *name) |
|
Line 60 macro_open(struct parse *p, const char *name) |
|
} |
} |
|
|
static void |
static void |
macro_close(struct parse *p) |
macro_close(struct format *p) |
{ |
{ |
assert(p->linestate == LINE_MACRO); |
assert(p->linestate == LINE_MACRO); |
putchar('\n'); |
putchar('\n'); |
Line 620 macro_close(struct parse *p) |
|
Line 68 macro_close(struct parse *p) |
|
} |
} |
|
|
static void |
static void |
macro_line(struct parse *p, const char *name) |
macro_line(struct format *p, const char *name) |
{ |
{ |
macro_open(p, name); |
macro_open(p, name); |
macro_close(p); |
macro_close(p); |
} |
} |
|
|
#define MACROLINE_UPPER 1 |
#define ARG_SPACE 1 /* Insert whitespace before this argument. */ |
#define MACROLINE_NOWS 2 |
#define ARG_SINGLE 2 /* Quote argument if it contains whitespace. */ |
|
#define ARG_QUOTED 4 /* We are already in a quoted argument. */ |
|
#define ARG_UPPER 8 /* Covert argument to upper case. */ |
/* |
/* |
* Print an argument string on a macro line, collapsing whitespace. |
* Print an argument string on a macro line, collapsing whitespace. |
*/ |
*/ |
static void |
static void |
macro_addarg(struct parse *p, const char *arg, int fl) |
macro_addarg(struct format *p, const char *arg, int flags) |
{ |
{ |
const char *cp; |
const char *cp; |
int wantspace; |
|
|
|
assert(p->linestate == LINE_MACRO); |
assert(p->linestate == LINE_MACRO); |
wantspace = !(fl & MACROLINE_NOWS); |
|
|
/* Quote if requested and necessary. */ |
|
|
|
if ((flags & (ARG_SINGLE | ARG_QUOTED)) == ARG_SINGLE) { |
|
for (cp = arg; *cp != '\0'; cp++) |
|
if (isspace((unsigned char)*cp)) |
|
break; |
|
if (*cp != '\0') { |
|
if (flags & ARG_SPACE) { |
|
putchar(' '); |
|
flags &= ~ ARG_SPACE; |
|
} |
|
putchar('"'); |
|
flags = ARG_QUOTED; |
|
} |
|
} |
|
|
for (cp = arg; *cp != '\0'; cp++) { |
for (cp = arg; *cp != '\0'; cp++) { |
|
|
|
/* Collapse whitespace. */ |
|
|
if (isspace((unsigned char)*cp)) { |
if (isspace((unsigned char)*cp)) { |
wantspace = 1; |
flags |= ARG_SPACE; |
continue; |
continue; |
} else if (wantspace) { |
} else if (flags & ARG_SPACE) { |
putchar(' '); |
putchar(' '); |
wantspace = 0; |
flags &= ~ ARG_SPACE; |
} |
} |
|
|
/* Escape us if we look like a macro. */ |
/* Escape us if we look like a macro. */ |
if ((cp == arg || cp[-1] == ' ') && |
|
|
if ((flags & ARG_QUOTED) == 0 && |
|
(cp == arg || isspace((unsigned char)cp[-1])) && |
isupper((unsigned char)cp[0]) && |
isupper((unsigned char)cp[0]) && |
islower((unsigned char)cp[1]) && |
islower((unsigned char)cp[1]) && |
(cp[2] == '\0' || cp[2] == ' ' || |
(cp[2] == '\0' || cp[2] == ' ' || |
(islower((unsigned char)cp[2]) && |
(islower((unsigned char)cp[2]) && |
(cp[3] == '\0' || cp[3] == ' ')))) |
(cp[3] == '\0' || cp[3] == ' ')))) |
fputs("\\&", stdout); |
fputs("\\&", stdout); |
if (fl & MACROLINE_UPPER) |
|
|
if (*cp == '"') |
|
fputs("\\(dq", stdout); |
|
else if (flags & ARG_UPPER) |
putchar(toupper((unsigned char)*cp)); |
putchar(toupper((unsigned char)*cp)); |
else |
else |
putchar(*cp); |
putchar(*cp); |
Line 665 macro_addarg(struct parse *p, const char *arg, int fl) |
|
Line 139 macro_addarg(struct parse *p, const char *arg, int fl) |
|
} |
} |
|
|
static void |
static void |
macro_argline(struct parse *p, const char *name, const char *arg) |
macro_argline(struct format *p, const char *name, const char *arg) |
{ |
{ |
macro_open(p, name); |
macro_open(p, name); |
macro_addarg(p, arg, 0); |
macro_addarg(p, arg, ARG_SPACE); |
macro_close(p); |
macro_close(p); |
} |
} |
|
|
/* |
/* |
* Recurse nodes to print arguments on a macro line. |
* Recursively append text from the children of a node to a macro line. |
*/ |
*/ |
static void |
static void |
macro_addnode(struct parse *p, struct pnode *pn, int fl) |
macro_addnode(struct format *p, struct pnode *pn, int flags) |
{ |
{ |
bufclear(p); |
int quote_now; |
bufappend_r(p, pn); |
|
macro_addarg(p, p->b, fl); |
assert(p->linestate == LINE_MACRO); |
|
|
|
/* |
|
* If the only child is a text node, just add that text, |
|
* letting macro_addarg() decide about quoting. |
|
*/ |
|
|
|
pn = TAILQ_FIRST(&pn->childq); |
|
if (pn != NULL && pn->node == NODE_TEXT && |
|
TAILQ_NEXT(pn, child) == NULL) { |
|
macro_addarg(p, pn->b, flags); |
|
return; |
|
} |
|
|
|
/* |
|
* If we want the argument quoted and are not already |
|
* in a quoted context, quote now. |
|
*/ |
|
|
|
quote_now = 0; |
|
if (flags & ARG_SINGLE) { |
|
if ((flags & ARG_QUOTED) == 0) { |
|
if (flags & ARG_SPACE) { |
|
putchar(' '); |
|
flags &= ~ARG_SPACE; |
|
} |
|
putchar('"'); |
|
flags |= ARG_QUOTED; |
|
quote_now = 1; |
|
} |
|
flags &= ~ARG_SINGLE; |
|
} |
|
|
|
/* |
|
* Iterate to child and sibling nodes, |
|
* inserting whitespace between nodes. |
|
*/ |
|
|
|
while (pn != NULL) { |
|
if (pn->node == NODE_TEXT) |
|
macro_addarg(p, pn->b, flags); |
|
else |
|
macro_addnode(p, pn, flags); |
|
pn = TAILQ_NEXT(pn, child); |
|
flags |= ARG_SPACE; |
|
} |
|
if (quote_now) |
|
putchar('"'); |
} |
} |
|
|
static void |
static void |
macro_nodeline(struct parse *p, const char *name, struct pnode *pn) |
macro_nodeline(struct format *p, const char *name, struct pnode *pn, int flags) |
{ |
{ |
macro_open(p, name); |
macro_open(p, name); |
macro_addnode(p, pn, 0); |
macro_addnode(p, pn, ARG_SPACE | flags); |
macro_close(p); |
macro_close(p); |
} |
} |
|
|
Line 696 macro_nodeline(struct parse *p, const char *name, stru |
|
Line 217 macro_nodeline(struct parse *p, const char *name, stru |
|
* emit the closing punctuation as a trailing macro argument. |
* emit the closing punctuation as a trailing macro argument. |
*/ |
*/ |
static void |
static void |
macro_closepunct(struct parse *p, struct pnode *pn) |
macro_closepunct(struct format *p, struct pnode *pn) |
{ |
{ |
if ((pn = TAILQ_NEXT(pn, child)) != NULL && |
if ((pn = TAILQ_NEXT(pn, child)) != NULL && |
pn->node == NODE_TEXT && pn->bsz > 0 && |
pn->node == NODE_TEXT && pn->bsz > 0 && |
Line 711 macro_closepunct(struct parse *p, struct pnode *pn) |
|
Line 232 macro_closepunct(struct parse *p, struct pnode *pn) |
|
} |
} |
|
|
static void |
static void |
print_text(struct parse *p, const char *word) |
print_text(struct format *p, const char *word) |
{ |
{ |
switch (p->linestate) { |
switch (p->linestate) { |
case LINE_NEW: |
case LINE_NEW: |
Line 728 print_text(struct parse *p, const char *word) |
|
Line 249 print_text(struct parse *p, const char *word) |
|
} |
} |
|
|
static void |
static void |
pnode_printpara(struct parse *p, struct pnode *pn) |
pnode_printpara(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
Line 755 pnode_printpara(struct parse *p, struct pnode *pn) |
|
Line 276 pnode_printpara(struct parse *p, struct pnode *pn) |
|
* 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 (pp->node == NODE_TITLE) { |
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; |
Line 780 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
Line 301 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
return; |
return; |
|
|
level = ++p->level; |
level = ++p->level; |
flags = level == 1 ? MACROLINE_UPPER : 0; |
flags = ARG_SPACE; |
|
if (level == 1) |
|
flags |= ARG_UPPER; |
if (level < 3) { |
if (level < 3) { |
switch (pn->node) { |
switch (pn->node) { |
case NODE_CAUTION: |
case NODE_CAUTION: |
Line 838 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
Line 361 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
macro_addnode(p, pp, flags); |
macro_addnode(p, pp, flags); |
pnode_unlink(pp); |
pnode_unlink(pp); |
} else |
} else |
macro_addarg(p, title, 0); |
macro_addarg(p, title, ARG_SPACE | ARG_QUOTED); |
macro_close(p); |
macro_close(p); |
} |
} |
|
|
Line 846 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
Line 369 pnode_printrefsect(struct parse *p, struct pnode *pn) |
|
* 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; |
|
|
Line 859 pnode_printciterefentry(struct parse *p, struct pnode |
|
Line 382 pnode_printciterefentry(struct parse *p, struct pnode |
|
} |
} |
macro_open(p, "Xr"); |
macro_open(p, "Xr"); |
if (title == NULL) |
if (title == NULL) |
macro_addarg(p, "unknown", 0); |
macro_addarg(p, "unknown", ARG_SPACE); |
else |
else |
macro_addnode(p, title, 0); |
macro_addnode(p, title, ARG_SPACE | ARG_SINGLE); |
if (manvol == NULL) |
if (manvol == NULL) |
macro_addarg(p, "1", 0); |
macro_addarg(p, "1", ARG_SPACE); |
else |
else |
macro_addnode(p, manvol, 0); |
macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); |
macro_close(p); |
macro_close(p); |
|
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; |
|
|
Line 883 pnode_printrefmeta(struct parse *p, struct pnode *pn) |
|
Line 407 pnode_printrefmeta(struct parse *p, struct pnode *pn) |
|
} |
} |
macro_open(p, "Dt"); |
macro_open(p, "Dt"); |
if (title == NULL) |
if (title == NULL) |
macro_addarg(p, "UNKNOWN", 0); |
macro_addarg(p, "UNKNOWN", ARG_SPACE); |
else |
else |
macro_addnode(p, title, MACROLINE_UPPER); |
macro_addnode(p, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
if (manvol == NULL) |
if (manvol == NULL) |
macro_addarg(p, "1", 0); |
macro_addarg(p, "1", ARG_SPACE); |
else |
else |
macro_addnode(p, manvol, 0); |
macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE); |
macro_close(p); |
macro_close(p); |
|
pnode_unlink(pn); |
} |
} |
|
|
static void |
static void |
pnode_printfuncdef(struct parse *p, struct pnode *pn) |
pnode_printfuncdef(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp, *ftype, *func; |
struct pnode *pp, *ftype, *func; |
|
|
Line 906 pnode_printfuncdef(struct parse *p, struct pnode *pn) |
|
Line 431 pnode_printfuncdef(struct parse *p, struct pnode *pn) |
|
func = pp; |
func = pp; |
} |
} |
if (ftype != NULL) |
if (ftype != NULL) |
macro_nodeline(p, "Ft", ftype); |
macro_argline(p, "Ft", ftype->b); |
macro_open(p, "Fo"); |
macro_open(p, "Fo"); |
if (func == NULL) |
if (func == NULL) |
macro_addarg(p, "UNKNOWN", 0); |
macro_addarg(p, "UNKNOWN", ARG_SPACE); |
else |
else |
macro_addnode(p, func, 0); |
macro_addnode(p, func, ARG_SPACE | ARG_SINGLE); |
macro_close(p); |
macro_close(p); |
} |
} |
|
|
static void |
|
pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp, *ptype, *param; |
|
int flags; |
|
|
|
ptype = param = NULL; |
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
|
if (pp->node == NODE_TEXT) |
|
ptype = pp; |
|
else if (pp->node == NODE_PARAMETER) |
|
param = pp; |
|
} |
|
macro_open(p, "Fa \""); |
|
flags = MACROLINE_NOWS; |
|
if (ptype != NULL) { |
|
macro_addnode(p, ptype, flags); |
|
flags = 0; |
|
} |
|
if (param != NULL) |
|
macro_addnode(p, param, flags); |
|
flags = MACROLINE_NOWS; |
|
macro_addarg(p, "\"", flags); |
|
macro_close(p); |
|
} |
|
|
|
/* |
/* |
* 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 948 pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
Line 447 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 961 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
Line 460 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
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 972 pnode_printmathfenced(struct parse *p, struct pnode *p |
|
Line 471 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 995 pnode_printmath(struct parse *p, struct pnode *pn) |
|
Line 494 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; |
|
|
Line 1013 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 513 pnode_printfuncprototype(struct parse *p, struct pnode |
|
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (pp->node == NODE_PARAMDEF) |
if (pp->node == NODE_PARAMDEF) |
pnode_printparamdef(p, pp); |
macro_nodeline(p, "Fa", pp, ARG_SINGLE); |
|
|
macro_line(p, "Fc"); |
macro_line(p, "Fc"); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
/* |
/* |
Line 1026 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 527 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 1049 pnode_printarg(struct parse *p, struct pnode *pn) |
|
Line 550 pnode_printarg(struct parse *p, struct pnode *pn) |
|
macro_open(p, "Ar"); |
macro_open(p, "Ar"); |
pnode_print(p, pp); |
pnode_print(p, pp); |
if (isrep && pp->node == NODE_TEXT) |
if (isrep && pp->node == NODE_TEXT) |
macro_addarg(p, "...", 0); |
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 1093 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 595 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
while (np != NULL) { |
while (np != NULL) { |
if (pp->node != np->node) |
if (pp->node != np->node) |
break; |
break; |
macro_addarg(p, "|", 0); |
macro_addarg(p, "|", ARG_SPACE); |
macro_addnode(p, np, 0); |
macro_addnode(p, np, ARG_SPACE); |
pp = np; |
pp = np; |
np = TAILQ_NEXT(np, child); |
np = TAILQ_NEXT(np, child); |
} |
} |
} |
} |
if (sv) |
if (sv) |
macro_close(p); |
macro_close(p); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printprologue(struct parse *p, struct pnode *pn) |
pnode_printprologue(struct format *p, struct ptree *tree) |
{ |
{ |
struct pnode *pp; |
struct pnode *refmeta; |
|
|
pp = p->root == NULL ? NULL : |
refmeta = tree->root == NULL ? NULL : |
pnode_findfirst(p->root, NODE_REFMETA); |
pnode_findfirst(tree->root, NODE_REFMETA); |
|
|
macro_line(p, "Dd $Mdocdate" "$"); |
macro_line(p, "Dd $Mdocdate" "$"); |
if (pp != NULL) { |
if (refmeta == NULL) { |
pnode_printrefmeta(p, pp); |
|
pnode_unlink(pp); |
|
} else { |
|
macro_open(p, "Dt"); |
macro_open(p, "Dt"); |
macro_addarg(p, |
macro_addarg(p, |
pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN"), 0); |
pnode_getattr_raw(tree->root, ATTRKEY_ID, "UNKNOWN"), |
macro_addarg(p, "1", 0); |
ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
|
macro_addarg(p, "1", ARG_SPACE); |
macro_close(p); |
macro_close(p); |
} |
} else |
|
pnode_printrefmeta(p, refmeta); |
macro_line(p, "Os"); |
macro_line(p, "Os"); |
|
|
if (p->flags & PARSE_EQN) { |
if (tree->flags & TREE_EQN) { |
macro_line(p, "EQ"); |
macro_line(p, "EQ"); |
print_text(p, "delim $$"); |
print_text(p, "delim $$"); |
macro_line(p, "EN"); |
macro_line(p, "EN"); |
Line 1136 pnode_printprologue(struct parse *p, struct pnode *pn) |
|
Line 638 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; |
Line 1146 pnode_printvarlistentry(struct parse *p, struct pnode |
|
Line 648 pnode_printvarlistentry(struct parse *p, struct pnode |
|
if (pp->node != NODE_TERM) |
if (pp->node != NODE_TERM) |
continue; |
continue; |
if ( ! first) |
if ( ! first) |
macro_addarg(p, ",", MACROLINE_NOWS); |
macro_addarg(p, ",", 0); |
pnode_print(p, pp); |
pnode_print(p, pp); |
first = 0; |
first = 0; |
} |
} |
Line 1154 pnode_printvarlistentry(struct parse *p, struct pnode |
|
Line 656 pnode_printvarlistentry(struct parse *p, struct pnode |
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (pp->node != NODE_TERM) |
if (pp->node != NODE_TERM) |
pnode_print(p, pp); |
pnode_print(p, pp); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printrow(struct parse *p, struct pnode *pn) |
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 |
|
pnode_printrow(struct format *p, struct pnode *pn) |
|
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
macro_line(p, "Bl -dash -compact"); |
macro_line(p, "Bl -dash -compact"); |
Line 1167 pnode_printrow(struct parse *p, struct pnode *pn) |
|
Line 684 pnode_printrow(struct parse *p, struct pnode *pn) |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
} |
} |
macro_line(p, "El"); |
macro_line(p, "El"); |
|
pnode_unlink(pn); |
} |
} |
|
|
static void |
static void |
pnode_printtable(struct parse *p, struct pnode *pn) |
pnode_printtable(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
pnode_printtitle(p, pn); |
if (pp->node == NODE_TITLE) { |
|
pnode_printpara(p, pp); |
|
pnode_print(p, pp); |
|
pnode_unlink(pp); |
|
} |
|
} |
|
macro_line(p, "Bl -ohang"); |
macro_line(p, "Bl -ohang"); |
while ((pp = pnode_findfirst(pn, NODE_ROW)) != NULL) { |
while ((pp = pnode_findfirst(pn, NODE_ROW)) != NULL) { |
macro_line(p, "It Table Row"); |
macro_line(p, "It Table Row"); |
pnode_printrow(p, pp); |
pnode_printrow(p, pp); |
pnode_unlink(pp); |
|
} |
} |
macro_line(p, "El"); |
macro_line(p, "El"); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
static void |
static void |
pnode_printlist(struct parse *p, struct pnode *pn) |
pnode_printlist(struct format *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
pnode_printtitle(p, pn); |
if (pp->node == NODE_TITLE) { |
|
pnode_printpara(p, pp); |
|
pnode_print(p, pp); |
|
pnode_unlink(pp); |
|
} |
|
} |
|
macro_argline(p, "Bl", |
macro_argline(p, "Bl", |
pn->node == NODE_ORDEREDLIST ? "-enum" : "-bullet"); |
pn->node == NODE_ORDEREDLIST ? "-enum" : "-bullet"); |
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
Line 1209 pnode_printlist(struct parse *p, struct pnode *pn) |
|
Line 715 pnode_printlist(struct parse *p, struct pnode *pn) |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
} |
} |
macro_line(p, "El"); |
macro_line(p, "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; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
pnode_printtitle(p, pn); |
if (pp->node == NODE_TITLE) { |
|
pnode_printpara(p, pp); |
|
pnode_print(p, pp); |
|
pnode_unlink(pp); |
|
} |
|
} |
|
macro_line(p, "Bl -tag -width Ds"); |
macro_line(p, "Bl -tag -width Ds"); |
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
if (pp->node == NODE_VARLISTENTRY) |
if (pp->node == NODE_VARLISTENTRY) |
pnode_print(p, pp); |
pnode_print(p, pp); |
else |
else |
macro_nodeline(p, "It", pp); |
macro_nodeline(p, "It", pp, 0); |
} |
} |
macro_line(p, "El"); |
macro_line(p, "El"); |
|
pnode_unlinksub(pn); |
} |
} |
|
|
/* |
/* |
Line 1240 pnode_printvariablelist(struct parse *p, struct pnode |
|
Line 742 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; |
const char *ccp; |
Line 1262 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 764 pnode_print(struct parse *p, struct pnode *pn) |
|
return; |
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: |
macro_open(p, "An"); |
macro_open(p, "An"); |
Line 1275 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 776 pnode_print(struct parse *p, struct pnode *pn) |
|
break; |
break; |
case NODE_CITEREFENTRY: |
case NODE_CITEREFENTRY: |
pnode_printciterefentry(p, pn); |
pnode_printciterefentry(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_CITETITLE: |
case NODE_CITETITLE: |
macro_open(p, "%T"); |
macro_open(p, "%T"); |
Line 1311 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 811 pnode_print(struct parse *p, struct pnode *pn) |
|
break; |
break; |
case NODE_FUNCPROTOTYPE: |
case NODE_FUNCPROTOTYPE: |
pnode_printfuncprototype(p, pn); |
pnode_printfuncprototype(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_FUNCSYNOPSISINFO: |
case NODE_FUNCSYNOPSISINFO: |
macro_open(p, "Fd"); |
macro_open(p, "Fd"); |
Line 1328 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 827 pnode_print(struct parse *p, struct pnode *pn) |
|
break; |
break; |
case NODE_ITEMIZEDLIST: |
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_KEYSYM: |
case NODE_KEYSYM: |
macro_open(p, "Sy"); |
macro_open(p, "Sy"); |
Line 1355 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 852 pnode_print(struct parse *p, struct pnode *pn) |
|
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 1369 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 865 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: |
macro_open(p, "Fl"); |
macro_open(p, "Fl"); |
break; |
break; |
case NODE_ORDEREDLIST: |
case NODE_ORDEREDLIST: |
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_PARAMETER: |
case NODE_PARAMETER: |
/* Suppress non-text children... */ |
macro_nodeline(p, "Fa", pn, ARG_SINGLE); |
macro_open(p, "Fa \""); |
|
macro_addnode(p, pn, MACROLINE_NOWS); |
|
macro_addarg(p, "\"", MACROLINE_NOWS); |
|
macro_close(p); |
|
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case NODE_QUOTE: |
case NODE_QUOTE: |
Line 1406 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 896 pnode_print(struct parse *p, struct pnode *pn) |
|
case NODE_REFNAME: |
case NODE_REFNAME: |
/* Suppress non-text children... */ |
/* Suppress non-text children... */ |
macro_open(p, "Nm"); |
macro_open(p, "Nm"); |
macro_addnode(p, pn, 0); |
macro_addnode(p, pn, ARG_SPACE | ARG_SINGLE); |
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case NODE_REFNAMEDIV: |
case NODE_REFNAMEDIV: |
Line 1417 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 907 pnode_print(struct parse *p, struct pnode *pn) |
|
break; |
break; |
case NODE_REFSYNOPSISDIV: |
case NODE_REFSYNOPSISDIV: |
pnode_printrefsynopsisdiv(p, pn); |
pnode_printrefsynopsisdiv(p, pn); |
macro_line(p, "Sh SYNOPSIS"); |
|
break; |
break; |
case NODE_PREFACE: |
case NODE_PREFACE: |
case NODE_SECTION: |
case NODE_SECTION: |
Line 1442 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 931 pnode_print(struct parse *p, struct pnode *pn) |
|
case NODE_TABLE: |
case NODE_TABLE: |
case NODE_INFORMALTABLE: |
case NODE_INFORMALTABLE: |
pnode_printtable(p, pn); |
pnode_printtable(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_TEXT: |
case NODE_TEXT: |
bufclear(p); |
if (pn->bsz == 0) { |
bufappend(p, pn); |
|
if (p->bsz == 0) { |
|
assert(pn->real != pn->b); |
assert(pn->real != pn->b); |
break; |
break; |
} |
} |
Line 1462 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 948 pnode_print(struct parse *p, struct pnode *pn) |
|
* XXX: all whitespace, including tabs (?). |
* XXX: all whitespace, including tabs (?). |
* Remember to escape control characters and escapes. |
* Remember to escape control characters and escapes. |
*/ |
*/ |
cp = p->b; |
cp = pn->b; |
|
|
/* |
/* |
* There's often a superfluous "-" in its <option> tags |
* There's often a superfluous "-" in its <option> tags |
Line 1501 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 987 pnode_print(struct parse *p, struct pnode *pn) |
|
break; |
break; |
case NODE_VARIABLELIST: |
case NODE_VARIABLELIST: |
pnode_printvariablelist(p, pn); |
pnode_printvariablelist(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_VARLISTENTRY: |
case NODE_VARLISTENTRY: |
pnode_printvarlistentry(p, pn); |
pnode_printvarlistentry(p, pn); |
pnode_unlinksub(pn); |
|
break; |
break; |
case NODE_VARNAME: |
case NODE_VARNAME: |
macro_open(p, "Va"); |
macro_open(p, "Va"); |
Line 1581 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1065 pnode_print(struct parse *p, struct pnode *pn) |
|
pn->parent->node == NODE_REFNAMEDIV && |
pn->parent->node == NODE_REFNAMEDIV && |
TAILQ_NEXT(pn, child) != NULL && |
TAILQ_NEXT(pn, child) != NULL && |
TAILQ_NEXT(pn, child)->node == NODE_REFNAME) |
TAILQ_NEXT(pn, child)->node == NODE_REFNAME) |
macro_addarg(p, ",", 0); |
macro_addarg(p, ",", ARG_SPACE); |
if (sv == LINE_NEW) |
if (sv == LINE_NEW) |
macro_close(p); |
macro_close(p); |
break; |
break; |
Line 1607 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1091 pnode_print(struct parse *p, struct pnode *pn) |
|
} |
} |
} |
} |
|
|
/* |
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 ((rc = XML_Parse(xp, b, ssz, 0 == ssz)) == 0) |
|
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.linestate = LINE_NEW; |
|
pnode_printprologue(&p, p.root); |
|
pnode_print(&p, p.root); |
|
if (p.linestate != LINE_NEW) |
|
putchar('\n'); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return rc != 0 && p.stop == 0; |
|
} |
|
|
|
/* 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 = 1; |
|
|
|
while ((ch = getopt(argc, argv, "W")) != -1) |
|
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 = strcmp(fname, "-") == 0 ? |
|
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 (fd == -1) |
|
perror(fname); |
|
else if ((buf = malloc(4096)) == NULL) |
|
perror(NULL); |
|
else if ((xp = XML_ParserCreate(NULL)) == NULL) |
|
perror(NULL); |
|
else if (readfile(xp, fd, buf, 4096, fname)) |
|
rc = 0; |
|
|
|
XML_ParserFree(xp); |
|
free(buf); |
|
if (fd != STDIN_FILENO) |
|
close(fd); |
|
return rc; |
|
|
|
usage: |
|
fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname); |
|
return 1; |
|
} |
} |