version 1.40, 2014/10/12 15:08:45 |
version 1.60, 2019/03/22 17:01:10 |
|
|
/* $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 |
|
|
enum nodeid node; /* current (NODE_ROOT if pre-tree) */ |
enum nodeid node; /* current (NODE_ROOT if pre-tree) */ |
const char *fname; /* filename */ |
const char *fname; /* filename */ |
int stop; /* should we stop now? */ |
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 *root; /* root of parse tree */ |
struct pnode *cur; /* current node in tree */ |
struct pnode *cur; /* current node in tree */ |
char *b; /* nil-terminated buffer for pre-print */ |
char *b; /* NUL-terminated buffer for pre-print */ |
size_t bsz; /* current length of b */ |
size_t bsz; /* current length of b */ |
size_t mbsz; /* max bsz allocation */ |
size_t mbsz; /* max bsz allocation */ |
|
int level; /* header level, starting at 1 */ |
int newln; /* output: are we on a fresh line */ |
int newln; /* output: are we on a fresh line */ |
}; |
}; |
|
|
struct node { |
struct node { |
const char *name; /* docbook element name */ |
const char *name; /* docbook element name */ |
unsigned int flags; |
enum nodeid node; /* docbook element to generate */ |
#define NODE_IGNTEXT 1 /* ignore all contained text */ |
|
}; |
}; |
|
|
TAILQ_HEAD(pnodeq, pnode); |
TAILQ_HEAD(pnodeq, pnode); |
|
|
|
|
static const char *attrkeys[ATTRKEY__MAX] = { |
static const char *attrkeys[ATTRKEY__MAX] = { |
"choice", |
"choice", |
|
"class", |
|
"close", |
"id", |
"id", |
|
"linkend", |
|
"open", |
"rep" |
"rep" |
}; |
}; |
|
|
static const char *attrvals[ATTRVAL__MAX] = { |
static const char *attrvals[ATTRVAL__MAX] = { |
|
"monospaced", |
"norepeat", |
"norepeat", |
"opt", |
"opt", |
"plain", |
"plain", |
Line 86 static const char *attrvals[ATTRVAL__MAX] = { |
|
Line 94 static const char *attrvals[ATTRVAL__MAX] = { |
|
"req" |
"req" |
}; |
}; |
|
|
static const struct node nodes[NODE__MAX] = { |
static const struct node nodes[] = { |
{ NULL, 0 }, |
{ "acronym", NODE_ACRONYM }, |
{ "acronym", 0 }, |
{ "affiliation", NODE_AFFILIATION }, |
{ "anchor", NODE_IGNTEXT }, |
{ "anchor", NODE_ANCHOR }, |
{ "application", 0 }, |
{ "application", NODE_APPLICATION }, |
{ "arg", 0 }, |
{ "arg", NODE_ARG }, |
{ "caution", NODE_IGNTEXT }, |
{ "author", NODE_AUTHOR }, |
{ "citerefentry", NODE_IGNTEXT }, |
{ "authorgroup", NODE_AUTHORGROUP }, |
{ "cmdsynopsis", NODE_IGNTEXT }, |
{ "blockquote", NODE_BLOCKQUOTE }, |
{ "code", 0 }, |
{ "book", NODE_BOOK }, |
{ "colspec", NODE_IGNTEXT }, |
{ "bookinfo", NODE_BOOKINFO }, |
{ "command", 0 }, |
{ "caution", NODE_CAUTION }, |
{ "constant", 0 }, |
{ "chapter", NODE_SECTION }, |
{ "copyright", NODE_IGNTEXT }, |
{ "citerefentry", NODE_CITEREFENTRY }, |
{ "date", 0 }, |
{ "citetitle", NODE_CITETITLE }, |
{ "emphasis", 0 }, |
{ "cmdsynopsis", NODE_CMDSYNOPSIS }, |
{ "entry", 0 }, |
{ "code", NODE_CODE }, |
{ "envar", 0 }, |
{ "colspec", NODE_COLSPEC }, |
{ "fieldsynopsis", NODE_IGNTEXT }, |
{ "command", NODE_COMMAND }, |
{ "filename", 0 }, |
{ "constant", NODE_CONSTANT }, |
{ "funcdef", 0 }, |
{ "copyright", NODE_COPYRIGHT }, |
{ "funcprototype", NODE_IGNTEXT }, |
{ "date", NODE_DATE }, |
{ "funcsynopsis", NODE_IGNTEXT }, |
{ "editor", NODE_EDITOR }, |
{ "funcsynopsisinfo", 0 }, |
{ "emphasis", NODE_EMPHASIS }, |
{ "function", 0 }, |
{ "entry", NODE_ENTRY }, |
{ "group", NODE_IGNTEXT }, |
{ "envar", NODE_ENVAR }, |
{ "holder", NODE_IGNTEXT }, |
{ "fieldsynopsis", NODE_FIELDSYNOPSIS }, |
{ "info", NODE_IGNTEXT }, |
{ "filename", NODE_FILENAME }, |
{ "informalequation", NODE_IGNTEXT }, |
{ "firstname", NODE_FIRSTNAME }, |
{ "informaltable", NODE_IGNTEXT }, |
{ "firstterm", NODE_FIRSTTERM }, |
{ "inlineequation", NODE_IGNTEXT }, |
{ "footnote", NODE_FOOTNOTE }, |
{ "itemizedlist", NODE_IGNTEXT }, |
{ "funcdef", NODE_FUNCDEF }, |
{ "link", 0 }, |
{ "funcprototype", NODE_FUNCPROTOTYPE }, |
{ "listitem", NODE_IGNTEXT }, |
{ "funcsynopsis", NODE_FUNCSYNOPSIS }, |
{ "literal", 0 }, |
{ "funcsynopsisinfo", NODE_FUNCSYNOPSISINFO }, |
{ "manvolnum", 0 }, |
{ "function", NODE_FUNCTION }, |
{ "mml:math", NODE_IGNTEXT }, |
{ "glossterm", NODE_GLOSSTERM }, |
{ "mml:mfenced", 0 }, |
{ "group", NODE_GROUP }, |
{ "mml:mfrac", 0 }, |
{ "holder", NODE_HOLDER }, |
{ "mml:mi", 0 }, |
{ "index", NODE_INDEX }, |
{ "mml:mn", 0 }, |
{ "indexterm", NODE_INDEXTERM }, |
{ "mml:mo", 0 }, |
{ "info", NODE_INFO }, |
{ "mml:mrow", 0 }, |
{ "informalequation", NODE_INFORMALEQUATION }, |
{ "mml:msub", 0 }, |
{ "informaltable", NODE_INFORMALTABLE }, |
{ "mml:msup", 0 }, |
{ "inlineequation", NODE_INLINEEQUATION }, |
{ "modifier", 0 }, |
{ "itemizedlist", NODE_ITEMIZEDLIST }, |
{ "note", NODE_IGNTEXT }, |
{ "keysym", NODE_KEYSYM }, |
{ "option", 0 }, |
{ "legalnotice", NODE_LEGALNOTICE }, |
{ "orderedlist", NODE_IGNTEXT }, |
{ "link", NODE_LINK }, |
{ "para", 0 }, |
{ "listitem", NODE_LISTITEM }, |
{ "paramdef", 0 }, |
{ "literal", NODE_LITERAL }, |
{ "parameter", 0 }, |
{ "literallayout", NODE_LITERALLAYOUT }, |
{ "programlisting", 0 }, |
{ "manvolnum", NODE_MANVOLNUM }, |
{ "prompt", 0 }, |
{ "member", NODE_MEMBER }, |
{ "quote", 0 }, |
{ "mml:math", NODE_MML_MATH }, |
{ "refclass", NODE_IGNTEXT }, |
{ "mml:mfenced", NODE_MML_MFENCED }, |
{ "refdescriptor", NODE_IGNTEXT }, |
{ "mml:mfrac", NODE_MML_MFRAC }, |
{ "refentry", NODE_IGNTEXT }, |
{ "mml:mi", NODE_MML_MI }, |
{ "refentryinfo", NODE_IGNTEXT }, |
{ "mml:mn", NODE_MML_MN }, |
{ "refentrytitle", 0 }, |
{ "mml:mo", NODE_MML_MO }, |
{ "refmeta", NODE_IGNTEXT }, |
{ "mml:mrow", NODE_MML_MROW }, |
{ "refmetainfo", NODE_IGNTEXT }, |
{ "mml:msub", NODE_MML_MSUB }, |
{ "refmiscinfo", NODE_IGNTEXT }, |
{ "mml:msup", NODE_MML_MSUP }, |
{ "refname", 0 }, |
{ "modifier", NODE_MODIFIER }, |
{ "refnamediv", NODE_IGNTEXT }, |
{ "note", NODE_NOTE }, |
{ "refpurpose", 0 }, |
{ "option", NODE_OPTION }, |
{ "refsect1", NODE_IGNTEXT }, |
{ "orderedlist", NODE_ORDEREDLIST }, |
{ "refsect2", NODE_IGNTEXT }, |
{ "orgname", NODE_ORGNAME }, |
{ "refsect3", NODE_IGNTEXT }, |
{ "othername", NODE_OTHERNAME }, |
{ "refsection", NODE_IGNTEXT }, |
{ "para", NODE_PARA }, |
{ "refsynopsisdiv", NODE_IGNTEXT }, |
{ "paramdef", NODE_PARAMDEF }, |
{ "replaceable", 0 }, |
{ "parameter", NODE_PARAMETER }, |
{ "row", NODE_IGNTEXT }, |
{ "part", NODE_SECTION }, |
{ "sbr", NODE_IGNTEXT }, |
{ "phrase", NODE_PHRASE }, |
{ "screen", NODE_IGNTEXT }, |
{ "preface", NODE_PREFACE }, |
{ "sgmltag", 0 }, |
{ "primary", NODE_PRIMARY }, |
{ "structname", 0 }, |
{ "programlisting", NODE_PROGRAMLISTING }, |
{ "synopsis", 0 }, |
{ "prompt", NODE_PROMPT }, |
{ "table", NODE_IGNTEXT }, |
{ "quote", NODE_QUOTE }, |
{ "tbody", NODE_IGNTEXT }, |
{ "refclass", NODE_REFCLASS }, |
{ "term", 0 }, |
{ "refdescriptor", NODE_REFDESCRIPTOR }, |
{ NULL, 0 }, |
{ "refentry", NODE_REFENTRY }, |
{ "tfoot", NODE_IGNTEXT }, |
{ "refentryinfo", NODE_REFENTRYINFO }, |
{ "tgroup", NODE_IGNTEXT }, |
{ "refentrytitle", NODE_REFENTRYTITLE }, |
{ "thead", NODE_IGNTEXT }, |
{ "refmeta", NODE_REFMETA }, |
{ "tip", NODE_IGNTEXT }, |
{ "refmetainfo", NODE_REFMETAINFO }, |
{ "title", 0 }, |
{ "refmiscinfo", NODE_REFMISCINFO }, |
{ "trademark", 0 }, |
{ "refname", NODE_REFNAME }, |
{ "type", 0 }, |
{ "refnamediv", NODE_REFNAMEDIV }, |
{ "ulink", 0 }, |
{ "refpurpose", NODE_REFPURPOSE }, |
{ "userinput", 0 }, |
{ "refsect1", NODE_SECTION }, |
{ "variablelist", NODE_IGNTEXT }, |
{ "refsect2", NODE_SECTION }, |
{ "varlistentry", NODE_IGNTEXT }, |
{ "refsect3", NODE_SECTION }, |
{ "varname", 0 }, |
{ "refsection", NODE_SECTION }, |
{ "warning", NODE_IGNTEXT }, |
{ "refsynopsisdiv", NODE_REFSYNOPSISDIV }, |
{ "wordasword", 0 }, |
{ "releaseinfo", NODE_RELEASEINFO }, |
{ "year", NODE_IGNTEXT }, |
{ "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 int warn = 0; |
Line 207 xml_char(void *arg, const XML_Char *p, int sz) |
|
Line 248 xml_char(void *arg, const XML_Char *p, int sz) |
|
if (ps->stop || NODE_ROOT == ps->node) |
if (ps->stop || NODE_ROOT == ps->node) |
return; |
return; |
|
|
/* Not supposed to be collecting text. */ |
|
assert(NULL != ps->cur); |
assert(NULL != ps->cur); |
if (NODE_IGNTEXT & nodes[ps->node].flags) |
|
return; |
|
|
|
/* |
/* |
* Are we in the midst of processing text? |
* Are we in the midst of processing text? |
Line 221 xml_char(void *arg, const XML_Char *p, int sz) |
|
Line 259 xml_char(void *arg, const XML_Char *p, int sz) |
|
*/ |
*/ |
if (NODE_TEXT != ps->node) { |
if (NODE_TEXT != ps->node) { |
for (i = 0; i < sz; i++) |
for (i = 0; i < sz; i++) |
if ( ! isspace((int)p[i])) |
if ( ! isspace((unsigned char)p[i])) |
break; |
break; |
if (i == sz) |
if (i == sz) |
return; |
return; |
Line 244 xml_char(void *arg, const XML_Char *p, int sz) |
|
Line 282 xml_char(void *arg, const XML_Char *p, int sz) |
|
|
|
/* Append to current buffer. */ |
/* Append to current buffer. */ |
assert(sz >= 0); |
assert(sz >= 0); |
ps->cur->b = realloc(ps->cur->b, |
ps->cur->b = realloc(ps->cur->b, |
ps->cur->bsz + (size_t)sz); |
ps->cur->bsz + (size_t)sz); |
if (NULL == ps->cur->b) { |
if (NULL == ps->cur->b) { |
perror(NULL); |
perror(NULL); |
Line 261 pnode_trim(struct pnode *pn) |
|
Line 299 pnode_trim(struct pnode *pn) |
|
|
|
assert(NODE_TEXT == pn->node); |
assert(NODE_TEXT == pn->node); |
for ( ; pn->bsz > 0; pn->bsz--) |
for ( ; pn->bsz > 0; pn->bsz--) |
if ( ! isspace((int)pn->b[pn->bsz - 1])) |
if ( ! isspace((unsigned char)pn->b[pn->bsz - 1])) |
break; |
break; |
} |
} |
|
|
|
|
xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts) |
xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts) |
{ |
{ |
struct parse *ps = arg; |
struct parse *ps = arg; |
enum nodeid node; |
const struct node *node; |
enum attrkey key; |
enum attrkey key; |
enum attrval val; |
enum attrval val; |
struct pnode *dat; |
struct pnode *dat; |
Line 303 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 341 xml_elem_start(void *arg, const XML_Char *name, const |
|
ps->node = ps->cur->node; |
ps->node = ps->cur->node; |
} |
} |
|
|
|
for (node = nodes; NULL != node->name; node++) |
for (node = 0; node < NODE__MAX; node++) |
if (0 == strcmp(node->name, name)) |
if (NULL == nodes[node].name) |
|
continue; |
|
else if (0 == strcmp(nodes[node].name, name)) |
|
break; |
break; |
|
|
if (NODE__MAX == node && NODE_ROOT == ps->node) { |
if (NULL == node->name) { |
return; |
if (NODE_ROOT == ps->node) |
} else if (NODE__MAX == node) { |
return; |
fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", |
fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", |
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
ps->fname, XML_GetCurrentLineNumber(ps->xml), |
XML_GetCurrentColumnNumber(ps->xml), name); |
XML_GetCurrentColumnNumber(ps->xml), name); |
ps->stop = 1; |
ps->stop = 1; |
Line 324 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 359 xml_elem_start(void *arg, const XML_Char *name, const |
|
XML_GetCurrentColumnNumber(ps->xml)); |
XML_GetCurrentColumnNumber(ps->xml)); |
ps->stop = 1; |
ps->stop = 1; |
return; |
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 (NODE_INLINEEQUATION == node->node) |
|
ps->flags |= PARSE_EQN; |
|
|
if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { |
if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { |
perror(NULL); |
perror(NULL); |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
} |
} |
|
|
dat->node = ps->node = node; |
dat->node = ps->node = node->node; |
dat->parent = ps->cur; |
dat->parent = ps->cur; |
TAILQ_INIT(&dat->childq); |
TAILQ_INIT(&dat->childq); |
TAILQ_INIT(&dat->attrq); |
TAILQ_INIT(&dat->attrq); |
Line 364 xml_elem_start(void *arg, const XML_Char *name, const |
|
Line 389 xml_elem_start(void *arg, const XML_Char *name, const |
|
if (0 == strcmp(*att, attrkeys[key])) |
if (0 == strcmp(*att, attrkeys[key])) |
break; |
break; |
if (ATTRKEY__MAX == key) { |
if (ATTRKEY__MAX == key) { |
if (warn) |
|
fprintf(stderr, "%s:%zu:%zu: warning: " |
|
"unknown attribute \"%s\"\n", |
|
ps->fname, |
|
XML_GetCurrentLineNumber(ps->xml), |
|
XML_GetCurrentColumnNumber(ps->xml), |
|
*att); |
|
continue; |
|
} else if ( ! isattrkey(node, key)) { |
|
if (warn) |
if (warn) |
fprintf(stderr, "%s:%zu:%zu: warning: " |
fprintf(stderr, "%s:%zu:%zu: warning: " |
"bad attribute \"%s\"\n", |
"unknown attribute \"%s\"\n", |
ps->fname, |
ps->fname, |
XML_GetCurrentLineNumber(ps->xml), |
XML_GetCurrentLineNumber(ps->xml), |
XML_GetCurrentColumnNumber(ps->xml), |
XML_GetCurrentColumnNumber(ps->xml), |
*att); |
*att); |
continue; |
continue; |
} |
} |
for (val = 0; val < ATTRVAL__MAX; val++) |
for (val = 0; val < ATTRVAL__MAX; val++) |
if (0 == strcmp(*(att + 1), attrvals[val])) |
if (0 == strcmp(*(att + 1), attrvals[val])) |
break; |
break; |
if (ATTRVAL__MAX != val && ! isattrval(key, val)) { |
|
if (warn) |
|
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 = calloc(1, sizeof(struct pattr)); |
pattr->key = key; |
pattr->key = key; |
pattr->val = val; |
pattr->val = val; |
Line 487 pnode_unlinksub(struct pnode *pn) |
|
Line 493 pnode_unlinksub(struct pnode *pn) |
|
} |
} |
|
|
/* |
/* |
|
* Retrieve an enumeration attribute from a node. |
|
* Return ATTRVAL__MAX if the node has no such attribute. |
|
*/ |
|
enum attrval |
|
pnode_getattr(struct pnode *pn, enum attrkey key) |
|
{ |
|
struct pattr *ap; |
|
|
|
TAILQ_FOREACH(ap, &pn->attrq, child) |
|
if (ap->key == key) |
|
return ap->val; |
|
return(ATTRVAL__MAX); |
|
} |
|
|
|
/* |
|
* Retrieve an attribute string from a node. |
|
* Return defval if the node has no such attribute. |
|
*/ |
|
const char * |
|
pnode_getattr_raw(struct pnode *pn, enum attrkey key, const char *defval) |
|
{ |
|
struct pattr *ap; |
|
|
|
TAILQ_FOREACH(ap, &pn->attrq, child) |
|
if (ap->key == key) |
|
return ATTRVAL__MAX == ap->val ? ap->rawval : |
|
attrvals[ap->val]; |
|
return(defval); |
|
} |
|
|
|
/* |
* Reset the lookaside buffer. |
* Reset the lookaside buffer. |
*/ |
*/ |
static void |
static void |
Line 499 bufclear(struct parse *p) |
|
Line 536 bufclear(struct parse *p) |
|
/* |
/* |
* Append NODE_TEXT contents to the current buffer, reallocating its |
* Append NODE_TEXT contents to the current buffer, reallocating its |
* size if necessary. |
* size if necessary. |
* The buffer is ALWAYS nil-terminated. |
* The buffer is ALWAYS NUL-terminated. |
*/ |
*/ |
static void |
static void |
bufappend(struct parse *p, struct pnode *pn) |
bufappend(struct parse *p, struct pnode *pn) |
Line 535 bufappend_r(struct parse *p, struct pnode *pn) |
|
Line 572 bufappend_r(struct parse *p, struct pnode *pn) |
|
bufappend_r(p, pp); |
bufappend_r(p, pp); |
} |
} |
|
|
/* |
/* |
* Recursively search and return the first instance of "node". |
* Recursively search and return the first instance of "node". |
*/ |
*/ |
static struct pnode * |
static struct pnode * |
Line 574 pnode_printmacrolinetext(struct parse *p, struct pnode |
|
Line 611 pnode_printmacrolinetext(struct parse *p, struct pnode |
|
|
|
/* Convert all space to spaces. */ |
/* Convert all space to spaces. */ |
for (cp = p->b; '\0' != *cp; cp++) |
for (cp = p->b; '\0' != *cp; cp++) |
if (isspace((int)*cp)) |
if (isspace((unsigned char)*cp)) |
*cp = ' '; |
*cp = ' '; |
|
|
for (cp = p->b; isspace((int)*cp); cp++) |
for (cp = p->b; isspace((unsigned char)*cp); cp++) |
/* Spin past whitespace (XXX: necessary?) */ ; |
/* Spin past whitespace (XXX: necessary?) */ ; |
for ( ; '\0' != *cp; cp++) { |
for ( ; '\0' != *cp; cp++) { |
/* Escape us if we look like a macro. */ |
/* Escape us if we look like a macro. */ |
if ((cp == p->b || ' ' == *(cp - 1)) && |
if ((cp == p->b || ' ' == *(cp - 1)) && |
isupper((int)*cp) && |
isupper((unsigned char)*cp) && |
'\0' != *(cp + 1) && |
'\0' != *(cp + 1) && |
islower((int)*(cp + 1)) && |
islower((unsigned char)*(cp + 1)) && |
('\0' == *(cp + 2) || |
('\0' == *(cp + 2) || |
' ' == *(cp + 2) || |
' ' == *(cp + 2) || |
(islower((int)*(cp + 2)) && |
(islower((unsigned char)*(cp + 2)) && |
('\0' == *(cp + 3) || |
('\0' == *(cp + 3) || |
' ' == *(cp + 3))))) |
' ' == *(cp + 3))))) |
fputs("\\&", stdout); |
fputs("\\&", stdout); |
if (MACROLINE_UPPER & fl) |
if (MACROLINE_UPPER & fl) |
putchar(toupper((int)*cp)); |
putchar(toupper((unsigned char)*cp)); |
else |
else |
putchar((int)*cp); |
putchar(*cp); |
/* If we're a character escape, escape us. */ |
/* If we're a character escape, escape us. */ |
if ('\\' == *cp) |
if ('\\' == *cp) |
putchar('e'); |
putchar('e'); |
|
|
pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv) |
pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv) |
{ |
{ |
/* We wouldn't have done anything anyway. */ |
/* We wouldn't have done anything anyway. */ |
if ( ! (sv && ! p->newln)) |
if ( ! (sv && ! p->newln)) |
return; |
return; |
|
|
/* No next node or it's not text. */ |
/* No next node or it's not text. */ |
Line 661 pnode_printmclosepunct(struct parse *p, struct pnode * |
|
Line 698 pnode_printmclosepunct(struct parse *p, struct pnode * |
|
} else if (NODE_TEXT != pn->node) { |
} else if (NODE_TEXT != pn->node) { |
pnode_printmclose(p, sv); |
pnode_printmclose(p, sv); |
return; |
return; |
} |
} |
|
|
/* Only do this for the comma/period. */ |
/* Only do this for the comma/period. */ |
if (pn->bsz > 0 && |
if (pn->bsz > 0 && |
(',' == pn->b[0] || '.' == pn->b[0]) && |
(',' == pn->b[0] || '.' == pn->b[0]) && |
(1 == pn->bsz || isspace((int)pn->b[1]))) { |
(1 == pn->bsz || isspace((unsigned char)pn->b[1]))) { |
putchar(' '); |
putchar(' '); |
putchar(pn->b[0]); |
putchar(pn->b[0]); |
pn->b++; |
pn->b++; |
pn->bsz--; |
pn->bsz--; |
} |
} |
|
|
putchar('\n'); |
putchar('\n'); |
p->newln = 1; |
p->newln = 1; |
} |
} |
|
|
|
static void |
|
pnode_printpara(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
|
|
assert(p->newln); |
|
if (NULL == pn->parent || NODE_LISTITEM == pn->parent->node) |
|
return; |
|
|
|
pp = TAILQ_PREV(pn, pnodeq, child); |
|
if (NULL == pp) |
|
pp = pn->parent; |
|
if ((NODE_SECTION != pp->node && NODE_PREFACE != pp->node) || |
|
2 < p->level) |
|
puts(".Pp"); |
|
} |
|
|
/* |
/* |
* If the SYNOPSIS macro has a superfluous title, kill it. |
* If the SYNOPSIS macro has a superfluous title, kill it. |
*/ |
*/ |
Line 685 pnode_printrefsynopsisdiv(struct parse *p, struct pnod |
|
Line 739 pnode_printrefsynopsisdiv(struct parse *p, struct pnod |
|
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TITLE == pp->node) { |
if (NODE_TITLE == pp->node) { |
pnode_unlink(pp); |
pnode_unlink(pp); |
return; |
return; |
|
|
pnode_printrefsect(struct parse *p, struct pnode *pn) |
pnode_printrefsect(struct parse *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
const char *title; |
|
int flags, level; |
|
|
|
if (NULL == pn->parent) |
|
return; |
|
|
|
level = ++p->level; |
|
flags = 1 == level ? MACROLINE_UPPER : 0; |
|
if (3 > level) { |
|
switch (pn->node) { |
|
case (NODE_CAUTION): |
|
case (NODE_NOTE): |
|
case (NODE_TIP): |
|
case (NODE_WARNING): |
|
level = 3; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TITLE == pp->node) |
if (NODE_TITLE == pp->node) |
break; |
break; |
|
|
switch (pn->node) { |
if (NULL == pp) { |
case (NODE_REFSECT1): |
switch (pn->node) { |
|
case (NODE_PREFACE): |
|
title = "Preface"; |
|
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; |
|
} |
|
} |
|
|
|
switch (level) { |
|
case (1): |
fputs(".Sh", stdout); |
fputs(".Sh", stdout); |
break; |
break; |
case (NODE_REFSECT2): |
case (2): |
fputs(".Ss", stdout); |
fputs(".Ss", stdout); |
break; |
break; |
case (NODE_REFSECT3): |
default: |
puts(".Pp"); |
pnode_printpara(p, pn); |
fputs(".Sy", stdout); |
fputs(".Sy", stdout); |
break; |
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: |
|
break; |
|
} |
} |
|
|
p->newln = 0; |
|
|
|
if (NULL != pp) { |
if (NULL != pp) { |
pnode_printmacrolinetext(p, pp, |
p->newln = 0; |
NODE_REFSECT1 == pn->node ? |
pnode_printmacrolinetext(p, pp, flags); |
MACROLINE_UPPER : 0); |
|
pnode_printmclose(p, 1); |
pnode_printmclose(p, 1); |
pnode_unlink(pp); |
pnode_unlink(pp); |
} else { |
} else |
puts(NODE_REFSECT1 == pn->node ? |
printf(" %s\n", title); |
"UNKNOWN" : "unknown"); |
|
p->newln = 1; |
|
} |
|
} |
} |
|
|
/* |
/* |
Line 788 pnode_printrefmeta(struct parse *p, struct pnode *pn) |
|
Line 863 pnode_printrefmeta(struct parse *p, struct pnode *pn) |
|
else if (NODE_REFENTRYTITLE == pp->node) |
else if (NODE_REFENTRYTITLE == pp->node) |
title = pp; |
title = pp; |
|
|
puts(".Dd $Mdocdate" "$"); |
|
fputs(".Dt", stdout); |
fputs(".Dt", stdout); |
p->newln = 0; |
p->newln = 0; |
|
|
Line 802 pnode_printrefmeta(struct parse *p, struct pnode *pn) |
|
Line 876 pnode_printrefmeta(struct parse *p, struct pnode *pn) |
|
p->newln = 1; |
p->newln = 1; |
} else |
} else |
pnode_printmacroline(p, manvol); |
pnode_printmacroline(p, manvol); |
|
|
puts(".Os"); |
|
} |
} |
|
|
static void |
static void |
Line 863 pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
Line 935 pnode_printparamdef(struct parse *p, struct pnode *pn) |
|
} |
} |
|
|
/* |
/* |
|
* 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 |
|
pnode_printmathfenced(struct parse *p, struct pnode *pn) |
|
{ |
|
struct pnode *pp; |
|
|
|
printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "(")); |
|
|
|
pp = TAILQ_FIRST(&pn->childq); |
|
pnode_print(p, pp); |
|
|
|
while (NULL != (pp = TAILQ_NEXT(pp, child))) { |
|
putchar(','); |
|
pnode_print(p, pp); |
|
} |
|
|
|
printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")")); |
|
} |
|
|
|
/* |
* These math nodes require special handling because they have infix |
* These math nodes require special handling because they have infix |
* syntax, instead of the usual prefix or prefix. |
* syntax, instead of the usual prefix or prefix. |
* So we need to break up the first and second child node with a |
* So we need to break up the first and second child node with a |
Line 878 pnode_printmath(struct parse *p, struct pnode *pn) |
|
Line 974 pnode_printmath(struct parse *p, struct pnode *pn) |
|
|
|
switch (pn->node) { |
switch (pn->node) { |
case (NODE_MML_MSUP): |
case (NODE_MML_MSUP): |
printf(" sup "); |
fputs(" sup ", stdout); |
break; |
break; |
case (NODE_MML_MFRAC): |
case (NODE_MML_MFRAC): |
printf(" over "); |
fputs(" over ", stdout); |
break; |
break; |
case (NODE_MML_MSUB): |
case (NODE_MML_MSUB): |
printf(" sub "); |
fputs(" sub ", stdout); |
break; |
break; |
default: |
default: |
break; |
break; |
Line 901 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 997 pnode_printfuncprototype(struct parse *p, struct pnode |
|
|
|
assert(p->newln); |
assert(p->newln); |
TAILQ_FOREACH(fdef, &pn->childq, child) |
TAILQ_FOREACH(fdef, &pn->childq, child) |
if (NODE_FUNCDEF == fdef->node) |
if (NODE_FUNCDEF == fdef->node) |
break; |
break; |
|
|
if (NULL != fdef) |
if (NULL != fdef) |
Line 909 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 1005 pnode_printfuncprototype(struct parse *p, struct pnode |
|
else |
else |
puts(".Fo UNKNOWN"); |
puts(".Fo UNKNOWN"); |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_PARAMDEF == pp->node) |
if (NODE_PARAMDEF == pp->node) |
pnode_printparamdef(p, pp); |
pnode_printparamdef(p, pp); |
|
|
Line 917 pnode_printfuncprototype(struct parse *p, struct pnode |
|
Line 1013 pnode_printfuncprototype(struct parse *p, struct pnode |
|
p->newln = 1; |
p->newln = 1; |
} |
} |
|
|
/* |
/* |
* 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 933 pnode_printarg(struct parse *p, struct pnode *pn) |
|
Line 1029 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 (ATTRKEY_CHOICE == ap->key && |
(ATTRVAL_PLAIN == ap->val || |
(ATTRVAL_PLAIN == ap->val || |
ATTRVAL_REQ == ap->val)) |
ATTRVAL_REQ == ap->val)) |
isop = 0; |
isop = 0; |
else if (ATTRKEY_REP == ap->key && |
else if (ATTRKEY_REP == ap->key && |
(ATTRVAL_REPEAT == ap->val)) |
(ATTRVAL_REPEAT == ap->val)) |
Line 951 pnode_printarg(struct parse *p, struct pnode *pn) |
|
Line 1047 pnode_printarg(struct parse *p, struct pnode *pn) |
|
if (NODE_TEXT == pp->node) { |
if (NODE_TEXT == pp->node) { |
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Ar", stdout); |
fputs("Ar", stdout); |
} |
} |
pnode_print(p, pp); |
pnode_print(p, pp); |
if (NODE_TEXT == pp->node && isrep) |
if (NODE_TEXT == pp->node && isrep) |
fputs("...", stdout); |
fputs("...", stdout); |
} |
} |
} |
} |
Line 966 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 1062 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
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 (ATTRKEY_CHOICE == ap->key && |
(ATTRVAL_PLAIN == ap->val || |
(ATTRVAL_PLAIN == ap->val || |
ATTRVAL_REQ == ap->val)) { |
ATTRVAL_REQ == ap->val)) { |
Line 974 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
Line 1070 pnode_printgroup(struct parse *p, struct pnode *pn) |
|
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->newln; |
pnode_printmopen(p); |
pnode_printmopen(p); |
if (isop) |
if (isop) |
fputs("Op", stdout); |
fputs("Op", stdout); |
else if (sv) |
else if (sv) |
fputs("No", stdout); |
fputs("No", stdout); |
Line 1018 pnode_printprologue(struct parse *p, struct pnode *pn) |
|
Line 1114 pnode_printprologue(struct parse *p, struct pnode *pn) |
|
pp = NULL == p->root ? NULL : |
pp = NULL == p->root ? NULL : |
pnode_findfirst(p->root, NODE_REFMETA); |
pnode_findfirst(p->root, NODE_REFMETA); |
|
|
|
puts(".Dd $Mdocdate" "$"); |
if (NULL != pp) { |
if (NULL != pp) { |
pnode_printrefmeta(p, pp); |
pnode_printrefmeta(p, pp); |
pnode_unlink(pp); |
pnode_unlink(pp); |
} else { |
} else |
puts(".\\\" Supplying bogus prologue..."); |
printf(".Dt %s 1\n", |
puts(".Dd $Mdocdate" "$"); |
pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN")); |
puts(".Dt UNKNOWN 1"); |
puts(".Os"); |
puts(".Os"); |
|
|
if (PARSE_EQN & p->flags) { |
|
puts(".EQ"); |
|
puts("delim $$"); |
|
puts(".EN"); |
} |
} |
} |
} |
|
|
|
/* |
|
* We can have multiple <term> elements within a <varlistentry>, which |
|
* we should comma-separate as list headers. |
|
*/ |
static void |
static void |
pnode_printvarlistentry(struct parse *p, struct pnode *pn) |
pnode_printvarlistentry(struct parse *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
int first = 1; |
|
|
assert(p->newln); |
assert(p->newln); |
|
fputs(".It", stdout); |
|
p->newln = 0; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TERM == pp->node) { |
if (NODE_TERM == pp->node) { |
assert(p->newln); |
if ( ! first) |
fputs(".It", stdout); |
putchar(','); |
p->newln = 0; |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_unlink(pp); |
pnode_unlink(pp); |
pnode_printmclose(p, 1); |
first = 0; |
return; |
} else |
} |
break; |
|
|
puts(".It"); |
putchar('\n'); |
p->newln = 1; |
p->newln = 1; |
} |
} |
|
|
Line 1075 pnode_printtable(struct parse *p, struct pnode *pn) |
|
Line 1183 pnode_printtable(struct parse *p, struct pnode *pn) |
|
assert(p->newln); |
assert(p->newln); |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TITLE == pp->node) { |
if (NODE_TITLE == pp->node) { |
puts(".Pp"); |
pnode_printpara(p, pp); |
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_unlink(pp); |
pnode_unlink(pp); |
} |
} |
Line 1099 pnode_printlist(struct parse *p, struct pnode *pn) |
|
Line 1207 pnode_printlist(struct parse *p, struct pnode *pn) |
|
assert(p->newln); |
assert(p->newln); |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TITLE == pp->node) { |
if (NODE_TITLE == pp->node) { |
puts(".Pp"); |
pnode_printpara(p, pp); |
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_unlink(pp); |
pnode_unlink(pp); |
} |
} |
Line 1108 pnode_printlist(struct parse *p, struct pnode *pn) |
|
Line 1216 pnode_printlist(struct parse *p, struct pnode *pn) |
|
if (NODE_ORDEREDLIST == pn->node) |
if (NODE_ORDEREDLIST == pn->node) |
puts(".Bl -enum"); |
puts(".Bl -enum"); |
else |
else |
puts(".Bl -item"); |
puts(".Bl -bullet"); |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
TAILQ_FOREACH(pp, &pn->childq, child) { |
assert(p->newln); |
assert(p->newln); |
Line 1128 pnode_printvariablelist(struct parse *p, struct pnode |
|
Line 1236 pnode_printvariablelist(struct parse *p, struct pnode |
|
assert(p->newln); |
assert(p->newln); |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (NODE_TITLE == pp->node) { |
if (NODE_TITLE == pp->node) { |
puts(".Pp"); |
pnode_printpara(p, pp); |
pnode_print(p, pp); |
pnode_print(p, pp); |
pnode_unlink(pp); |
pnode_unlink(pp); |
} |
} |
|
|
pnode_print(struct parse *p, struct pnode *pn) |
pnode_print(struct parse *p, struct pnode *pn) |
{ |
{ |
struct pnode *pp; |
struct pnode *pp; |
|
const char *ccp; |
char *cp; |
char *cp; |
int last, sv; |
int last, sv; |
|
|
Line 1166 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1275 pnode_print(struct parse *p, struct pnode *pn) |
|
|
|
sv = p->newln; |
sv = p->newln; |
|
|
|
/* XXX fprintf(stderr, "NODE %s\n", nodes[pn->node].name); */ |
switch (pn->node) { |
switch (pn->node) { |
case (NODE_APPLICATION): |
case (NODE_APPLICATION): |
pnode_printmopen(p); |
pnode_printmopen(p); |
Line 1178 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1288 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_printarg(p, pn); |
pnode_printarg(p, pn); |
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
|
case (NODE_AUTHOR): |
|
pnode_printmopen(p); |
|
fputs("An", stdout); |
|
break; |
|
case (NODE_AUTHORGROUP): |
|
assert(p->newln); |
|
puts(".An -split"); |
|
break; |
|
case (NODE_BOOKINFO): |
|
assert(p->newln); |
|
puts(".Sh NAME"); |
|
break; |
case (NODE_CITEREFENTRY): |
case (NODE_CITEREFENTRY): |
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Xr", stdout); |
fputs("Xr", stdout); |
Line 1196 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1318 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Dv", stdout); |
fputs("Dv", stdout); |
break; |
break; |
|
case (NODE_EDITOR): |
|
puts("editor: "); |
|
pnode_printmopen(p); |
|
fputs("An", stdout); |
|
break; |
case (NODE_EMPHASIS): |
case (NODE_EMPHASIS): |
|
case (NODE_FIRSTTERM): |
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Em", stdout); |
fputs("Em", stdout); |
break; |
break; |
Line 1221 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1349 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Fd", stdout); |
fputs("Fd", stdout); |
break; |
break; |
|
case (NODE_INDEXTERM): |
|
return; |
|
case (NODE_INFORMALEQUATION): |
|
if ( ! p->newln) |
|
putchar('\n'); |
|
puts(".EQ"); |
|
p->newln = 0; |
|
break; |
|
case (NODE_INLINEEQUATION): |
|
fputc('$', stdout); |
|
p->newln = 0; |
|
break; |
case (NODE_ITEMIZEDLIST): |
case (NODE_ITEMIZEDLIST): |
assert(p->newln); |
assert(p->newln); |
pnode_printlist(p, pn); |
pnode_printlist(p, pn); |
Line 1230 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1370 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_printgroup(p, pn); |
pnode_printgroup(p, pn); |
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
|
case (NODE_LEGALNOTICE): |
|
assert(p->newln); |
|
puts(".Sh LEGAL NOTICE"); |
|
break; |
|
case (NODE_LINK): |
|
ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL); |
|
if (NULL == ccp) |
|
break; |
|
pnode_printmopen(p); |
|
printf("Sx %s\n", ccp); |
|
p->newln = 1; |
|
return; |
case (NODE_LITERAL): |
case (NODE_LITERAL): |
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Li", stdout); |
fputs("Li", stdout); |
break; |
break; |
case (NODE_MML_MATH): |
case (NODE_LITERALLAYOUT): |
if ( ! p->newln) |
assert(p->newln); |
putchar('\n'); |
puts(".Bd -literal"); |
puts(".EQ"); |
|
p->newln = 0; |
|
break; |
break; |
case (NODE_MML_MFENCED): |
case (NODE_MML_MFENCED): |
printf("left {"); |
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): |
case (NODE_MML_MN): |
case (NODE_MML_MN): |
case (NODE_MML_MO): |
case (NODE_MML_MO): |
putchar('{'); |
if (TAILQ_EMPTY(&pn->childq)) |
|
break; |
|
fputs(" { ", stdout); |
break; |
break; |
case (NODE_MML_MFRAC): |
case (NODE_MML_MFRAC): |
case (NODE_MML_MSUB): |
case (NODE_MML_MSUB): |
Line 1265 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1418 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_unlinksub(pn); |
pnode_unlinksub(pn); |
break; |
break; |
case (NODE_PARA): |
case (NODE_PARA): |
assert(p->newln); |
pnode_printpara(p, pn); |
if (NULL != pn->parent && |
|
NODE_LISTITEM == pn->parent->node) |
|
break; |
|
puts(".Pp"); |
|
break; |
break; |
case (NODE_PARAMETER): |
case (NODE_PARAMETER): |
/* Suppress non-text children... */ |
/* Suppress non-text children... */ |
Line 1284 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1433 pnode_print(struct parse *p, struct pnode *pn) |
|
fputs("Qo", stdout); |
fputs("Qo", stdout); |
break; |
break; |
case (NODE_PROGRAMLISTING): |
case (NODE_PROGRAMLISTING): |
/* FALLTHROUGH */ |
|
case (NODE_SCREEN): |
case (NODE_SCREEN): |
assert(p->newln); |
assert(p->newln); |
puts(".Bd -literal"); |
printf(".Bd %s\n", ATTRVAL_MONOSPACED == |
|
pnode_getattr(pn, ATTRKEY_CLASS) ? |
|
"-literal" : "-unfilled"); |
break; |
break; |
case (NODE_REFENTRYINFO): |
case (NODE_REFENTRYINFO): |
/* Suppress. */ |
/* Suppress. */ |
Line 1318 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1468 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_printrefsynopsisdiv(p, pn); |
pnode_printrefsynopsisdiv(p, pn); |
puts(".Sh SYNOPSIS"); |
puts(".Sh SYNOPSIS"); |
break; |
break; |
case (NODE_REFSECT1): |
case (NODE_PREFACE): |
/* FALLTHROUGH */ |
case (NODE_SECTION): |
case (NODE_REFSECT2): |
|
/* FALLTHROUGH */ |
|
case (NODE_REFSECT3): |
|
/* FALLTHROUGH */ |
|
case (NODE_REFSECTION): |
|
/* FALLTHROUGH */ |
|
case (NODE_NOTE): |
case (NODE_NOTE): |
/* FALLTHROUGH */ |
|
case (NODE_TIP): |
case (NODE_TIP): |
/* FALLTHROUGH */ |
|
case (NODE_CAUTION): |
case (NODE_CAUTION): |
/* FALLTHROUGH */ |
|
case (NODE_WARNING): |
case (NODE_WARNING): |
assert(p->newln); |
assert(p->newln); |
pnode_printrefsect(p, pn); |
pnode_printrefsect(p, pn); |
Line 1373 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1514 pnode_print(struct parse *p, struct pnode *pn) |
|
|
|
/* |
/* |
* Output all characters, squeezing out whitespace |
* Output all characters, squeezing out whitespace |
* between newlines. |
* between newlines. |
* XXX: all whitespace, including tabs (?). |
* XXX: all whitespace, including tabs (?). |
* Remember to escape control characters and escapes. |
* Remember to escape control characters and escapes. |
*/ |
*/ |
Line 1392 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1533 pnode_print(struct parse *p, struct pnode *pn) |
|
for (last = '\n'; '\0' != *cp; ) { |
for (last = '\n'; '\0' != *cp; ) { |
if ('\n' == last) { |
if ('\n' == last) { |
/* Consume all whitespace. */ |
/* Consume all whitespace. */ |
if (isspace((int)*cp)) { |
if (isspace((unsigned char)*cp)) { |
while (isspace((int)*cp)) |
while (isspace((unsigned char)*cp)) |
cp++; |
cp++; |
continue; |
continue; |
} else if ('\'' == *cp || '.' == *cp) |
} else if ('\'' == *cp || '.' == *cp) |
Line 1406 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1547 pnode_print(struct parse *p, struct pnode *pn) |
|
} |
} |
p->newln = 0; |
p->newln = 0; |
break; |
break; |
|
case (NODE_TITLE): |
|
if (pn->parent->node == NODE_BOOKINFO) { |
|
pnode_printmopen(p); |
|
fputs("Nd", stdout); |
|
} |
|
break; |
case (NODE_TYPE): |
case (NODE_TYPE): |
pnode_printmopen(p); |
pnode_printmopen(p); |
fputs("Vt", stdout); |
fputs("Vt", stdout); |
Line 1435 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1582 pnode_print(struct parse *p, struct pnode *pn) |
|
pnode_print(p, pp); |
pnode_print(p, pp); |
|
|
switch (pn->node) { |
switch (pn->node) { |
case (NODE_MML_MATH): |
case (NODE_INFORMALEQUATION): |
if ( ! p->newln) |
if ( ! p->newln) |
putchar('\n'); |
putchar('\n'); |
puts(".EN"); |
puts(".EN"); |
p->newln = 1; |
p->newln = 1; |
break; |
break; |
case (NODE_MML_MFENCED): |
case (NODE_INLINEEQUATION): |
printf("right }"); |
fputs("$ ", stdout); |
|
p->newln = sv; |
break; |
break; |
case (NODE_MML_MROW): |
case (NODE_MML_MROW): |
case (NODE_MML_MI): |
case (NODE_MML_MI): |
case (NODE_MML_MN): |
case (NODE_MML_MN): |
case (NODE_MML_MO): |
case (NODE_MML_MO): |
putchar('}'); |
if (TAILQ_EMPTY(&pn->childq)) |
|
break; |
|
fputs(" } ", stdout); |
break; |
break; |
case (NODE_APPLICATION): |
case (NODE_APPLICATION): |
case (NODE_ARG): |
case (NODE_ARG): |
|
case (NODE_AUTHOR): |
case (NODE_CITEREFENTRY): |
case (NODE_CITEREFENTRY): |
case (NODE_CODE): |
case (NODE_CODE): |
case (NODE_COMMAND): |
case (NODE_COMMAND): |
case (NODE_CONSTANT): |
case (NODE_CONSTANT): |
|
case (NODE_EDITOR): |
case (NODE_EMPHASIS): |
case (NODE_EMPHASIS): |
case (NODE_ENVAR): |
case (NODE_ENVAR): |
case (NODE_FILENAME): |
case (NODE_FILENAME): |
|
case (NODE_FIRSTTERM): |
case (NODE_FUNCTION): |
case (NODE_FUNCTION): |
case (NODE_FUNCSYNOPSISINFO): |
case (NODE_FUNCSYNOPSISINFO): |
case (NODE_LITERAL): |
case (NODE_LITERAL): |
Line 1487 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1640 pnode_print(struct parse *p, struct pnode *pn) |
|
* <refname> macros in sequence, then print out a |
* <refname> macros in sequence, then print out a |
* trailing comma before the newline. |
* trailing comma before the newline. |
*/ |
*/ |
if (NULL != pn->parent && |
if (NULL != pn->parent && |
NODE_REFNAMEDIV == pn->parent->node && |
NODE_REFNAMEDIV == pn->parent->node && |
NULL != TAILQ_NEXT(pn, child) && |
NULL != TAILQ_NEXT(pn, child) && |
NODE_REFNAME == TAILQ_NEXT(pn, child)->node) |
NODE_REFNAME == TAILQ_NEXT(pn, child)->node) |
fputs(" ,", stdout); |
fputs(" ,", stdout); |
pnode_printmclose(p, sv); |
pnode_printmclose(p, sv); |
break; |
break; |
|
case (NODE_PREFACE): |
|
case (NODE_SECTION): |
|
case (NODE_NOTE): |
|
case (NODE_TIP): |
|
case (NODE_CAUTION): |
|
case (NODE_WARNING): |
|
p->level--; |
|
break; |
|
case (NODE_LITERALLAYOUT): |
|
/* FALLTHROUGH */ |
case (NODE_PROGRAMLISTING): |
case (NODE_PROGRAMLISTING): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (NODE_SCREEN): |
case (NODE_SCREEN): |
Line 1501 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1664 pnode_print(struct parse *p, struct pnode *pn) |
|
puts(".Ed"); |
puts(".Ed"); |
p->newln = 1; |
p->newln = 1; |
break; |
break; |
|
case (NODE_TITLE): |
|
if (pn->parent->node == NODE_BOOKINFO) { |
|
pnode_printmclose(p, 1); |
|
puts(".Sh AUTHORS"); |
|
} |
|
break; |
default: |
default: |
break; |
break; |
} |
} |
Line 1511 pnode_print(struct parse *p, struct pnode *pn) |
|
Line 1680 pnode_print(struct parse *p, struct pnode *pn) |
|
* Invoke the parser context with each buffer fill. |
* Invoke the parser context with each buffer fill. |
*/ |
*/ |
static int |
static int |
readfile(XML_Parser xp, int fd, |
readfile(XML_Parser xp, int fd, |
char *b, size_t bsz, const char *fn) |
char *b, size_t bsz, const char *fn) |
{ |
{ |
struct parse p; |
struct parse p; |
Line 1532 readfile(XML_Parser xp, int fd, |
|
Line 1701 readfile(XML_Parser xp, int fd, |
|
if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz))) |
if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz))) |
fprintf(stderr, "%s:%zu:%zu: %s\n", fn, |
fprintf(stderr, "%s:%zu:%zu: %s\n", fn, |
XML_GetCurrentLineNumber(xp), |
XML_GetCurrentLineNumber(xp), |
XML_GetCurrentColumnNumber(xp), |
XML_GetCurrentColumnNumber(xp), |
XML_ErrorString |
XML_ErrorString |
(XML_GetErrorCode(xp))); |
(XML_GetErrorCode(xp))); |
else if ( ! p.stop && ssz > 0) |
else if ( ! p.stop && ssz > 0) |
continue; |
continue; |
/* |
/* |
* Exit when we've read all or errors have occured |
* Exit when we've read all or errors have occured |
* during the parse sequence. |
* during the parse sequence. |
*/ |
*/ |
Line 1588 main(int argc, char *argv[]) |
|
Line 1757 main(int argc, char *argv[]) |
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
if (argc > 1) |
if (argc > 1) { |
return(EXIT_FAILURE); |
fprintf(stderr, "%s: Too many arguments\n", argv[1]); |
else if (argc > 0) |
goto usage; |
|
} else if (argc > 0) |
fname = argv[0]; |
fname = argv[0]; |
|
|
/* Read from stdin or a file. */ |
/* Read from stdin or a file. */ |
fd = 0 == strcmp(fname, "-") ? |
fd = 0 == strcmp(fname, "-") ? |
STDIN_FILENO : open(fname, O_RDONLY, 0); |
STDIN_FILENO : open(fname, O_RDONLY, 0); |
|
|
/* |
/* |
Line 1619 main(int argc, char *argv[]) |
|
Line 1789 main(int argc, char *argv[]) |
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
|
|
usage: |
usage: |
fprintf(stderr, "usage: %s [-W]\n", progname); |
fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname); |
return(EXIT_FAILURE); |
return(EXIT_FAILURE); |
} |
} |