version 1.3, 2014/03/28 02:46:40 |
version 1.116, 2019/04/13 13:06:35 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
#include <sys/queue.h> |
|
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <expat.h> |
|
#include <fcntl.h> |
|
#include <getopt.h> |
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
/* |
#include "node.h" |
* All recognised node types. |
#include "macro.h" |
*/ |
#include "format.h" |
enum nodeid { |
|
NODE_ROOT = 0, /* Must comes first. */ |
|
/* Alpha-ordered hereafter. */ |
|
NODE_CITEREFENTRY, |
|
NODE_CODE, |
|
NODE_FUNCDEF, |
|
NODE_FUNCPROTOTYPE, |
|
NODE_FUNCSYNOPSIS, |
|
NODE_FUNCSYNOPSISINFO, |
|
NODE_FUNCTION, |
|
NODE_MANVOLNUM, |
|
NODE_PARA, |
|
NODE_PARAMDEF, |
|
NODE_PARAMETER, |
|
NODE_PROGRAMLISTING, |
|
NODE_REFCLASS, |
|
NODE_REFDESCRIPTOR, |
|
NODE_REFENTRY, |
|
NODE_REFENTRYTITLE, |
|
NODE_REFMETA, |
|
NODE_REFMISCINFO, |
|
NODE_REFNAME, |
|
NODE_REFNAMEDIV, |
|
NODE_REFPURPOSE, |
|
NODE_REFSECT1, |
|
NODE_REFSYNOPSISDIV, |
|
NODE_SYNOPSIS, |
|
NODE_TEXT, |
|
NODE_TITLE, |
|
NODE__MAX |
|
}; |
|
|
|
/* |
/* |
* Global parse state. |
* The implementation of the mdoc(7) formatter. |
* Keep this as simple and small as possible. |
|
*/ |
*/ |
struct parse { |
|
enum nodeid node; /* current (NODE_ROOT if pre-tree) */ |
|
int stop; /* should we stop now? */ |
|
struct pnode *root; /* root of parse tree */ |
|
struct pnode *cur; /* current node in tree */ |
|
char *b; |
|
size_t bsz; |
|
size_t mbsz; |
|
}; |
|
|
|
struct node { |
static void pnode_print(struct format *, struct pnode *); |
const char *name; |
|
unsigned int flags; |
|
#define NODE_IGNTEXT 1 /* ignore all contained text */ |
|
}; |
|
|
|
TAILQ_HEAD(pnodeq, pnode); |
|
|
|
struct pnode { |
static void |
enum nodeid node; /* node type */ |
pnode_printtext(struct format *f, struct pnode *n) |
char *b; /* binary data buffer */ |
|
size_t bsz; /* data buffer size */ |
|
struct pnode *parent; /* parent (or NULL if top) */ |
|
struct pnodeq childq; /* queue of children */ |
|
TAILQ_ENTRY(pnode) child; |
|
}; |
|
|
|
static const struct node nodes[NODE__MAX] = { |
|
{ NULL, 0 }, |
|
{ "citerefentry", NODE_IGNTEXT }, |
|
{ "code", 0 }, |
|
{ "funcdef", 0 }, |
|
{ "funcprototype", NODE_IGNTEXT }, |
|
{ "funcsynopsis", NODE_IGNTEXT }, |
|
{ "funcsynopsisinfo", 0 }, |
|
{ "function", 0 }, |
|
{ "manvolnum", 0 }, |
|
{ "para", 0 }, |
|
{ "paramdef", 0 }, |
|
{ "parameter", 0 }, |
|
{ "programlisting", 0 }, |
|
{ "refclass", NODE_IGNTEXT }, |
|
{ "refdescriptor", NODE_IGNTEXT }, |
|
{ "refentry", NODE_IGNTEXT }, |
|
{ "refentrytitle", 0 }, |
|
{ "refmeta", NODE_IGNTEXT }, |
|
{ "refmiscinfo", NODE_IGNTEXT }, |
|
{ "refname", 0 }, |
|
{ "refnamediv", NODE_IGNTEXT }, |
|
{ "refpurpose", 0 }, |
|
{ "refsect1", 0 }, |
|
{ "refsynopsisdiv", NODE_IGNTEXT }, |
|
{ "synopsis", 0 }, |
|
{ NULL, 0 }, |
|
{ "title", 0 }, |
|
}; |
|
|
|
/* |
|
* Look up whether "parent" is a valid parent for "node". |
|
*/ |
|
static int |
|
isparent(enum nodeid node, enum nodeid parent) |
|
{ |
{ |
|
struct pnode *nn; |
|
char *cp; |
|
int accept_arg; |
|
|
switch (node) { |
cp = n->b; |
case (NODE_ROOT): |
accept_arg = f->flags & FMT_ARG; |
return(0); |
if (f->linestate == LINE_MACRO && !n->spc && !accept_arg) { |
case (NODE_CITEREFENTRY): |
for (;;) { |
switch (parent) { |
if (*cp == '\0') |
case (NODE_FUNCSYNOPSISINFO): |
return; |
case (NODE_PARA): |
if (strchr("!),.:;?]", *cp) == NULL) |
case (NODE_PROGRAMLISTING): |
break; |
case (NODE_REFDESCRIPTOR): |
printf(" %c", *cp++); |
case (NODE_REFENTRYTITLE): |
|
case (NODE_REFNAME): |
|
case (NODE_REFPURPOSE): |
|
case (NODE_SYNOPSIS): |
|
case (NODE_TITLE): |
|
return(1); |
|
default: |
|
break; |
|
} |
} |
return(0); |
if (isspace((unsigned char)*cp)) { |
case (NODE_CODE): |
while (isspace((unsigned char)*cp)) |
switch (parent) { |
cp++; |
case (NODE_FUNCSYNOPSISINFO): |
macro_close(f); |
case (NODE_PARA): |
} else { |
case (NODE_PROGRAMLISTING): |
fputs(" Ns", stdout); |
case (NODE_REFDESCRIPTOR): |
f->flags &= FMT_IMPL; |
case (NODE_REFENTRYTITLE): |
accept_arg = 1; |
case (NODE_REFNAME): |
|
case (NODE_REFPURPOSE): |
|
case (NODE_SYNOPSIS): |
|
case (NODE_TITLE): |
|
return(1); |
|
default: |
|
break; |
|
} |
} |
return(0); |
} |
case (NODE_FUNCDEF): |
if (f->linestate == LINE_MACRO && !accept_arg && |
return(NODE_FUNCPROTOTYPE == parent); |
(f->flags & (FMT_CHILD | FMT_IMPL)) == 0) |
case (NODE_FUNCPROTOTYPE): |
macro_close(f); |
return(NODE_FUNCSYNOPSIS == parent); |
|
case (NODE_FUNCSYNOPSIS): |
/* |
switch (parent) { |
* Text preceding a macro without intervening whitespace |
case (NODE_PARA): |
* requires a .Pf macro. |
case (NODE_REFSECT1): |
* Set the spacing flag to avoid a redundant .Ns macro. |
case (NODE_REFSYNOPSISDIV): |
*/ |
return(1); |
|
default: |
if (f->linestate != LINE_MACRO && |
|
(nn = TAILQ_NEXT(n, child)) != NULL && nn->spc == 0) { |
|
switch (pnode_class(nn->node)) { |
|
case CLASS_LINE: |
|
case CLASS_ENCL: |
|
macro_open(f, "Pf"); |
|
accept_arg = 1; |
|
f->flags |= FMT_CHILD; |
|
nn->spc = 1; |
break; |
break; |
} |
|
return(0); |
|
case (NODE_FUNCSYNOPSISINFO): |
|
return(NODE_FUNCSYNOPSIS == parent); |
|
case (NODE_FUNCTION): |
|
switch (parent) { |
|
case (NODE_CODE): |
|
case (NODE_FUNCDEF): |
|
case (NODE_FUNCSYNOPSISINFO): |
|
case (NODE_PARA): |
|
case (NODE_REFDESCRIPTOR): |
|
case (NODE_REFENTRYTITLE): |
|
case (NODE_REFNAME): |
|
case (NODE_REFPURPOSE): |
|
case (NODE_SYNOPSIS): |
|
case (NODE_TITLE): |
|
return(1); |
|
default: |
default: |
break; |
break; |
} |
} |
return(0); |
} |
case (NODE_MANVOLNUM): |
|
switch (parent) { |
switch (f->linestate) { |
case (NODE_CITEREFENTRY): |
case LINE_NEW: |
case (NODE_REFMETA): |
break; |
return(1); |
case LINE_TEXT: |
default: |
if (n->spc) { |
break; |
if (n->node == NODE_TEXT) |
|
macro_close(f); |
|
else |
|
putchar(' '); |
} |
} |
return(0); |
|
case (NODE_PARA): |
|
switch (parent) { |
|
case (NODE_REFSECT1): |
|
case (NODE_REFSYNOPSISDIV): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
return(0); |
|
case (NODE_PARAMDEF): |
|
return(NODE_FUNCPROTOTYPE == parent); |
|
case (NODE_PARAMETER): |
|
switch (parent) { |
|
case (NODE_CODE): |
|
case (NODE_FUNCSYNOPSISINFO): |
|
case (NODE_PARA): |
|
case (NODE_PARAMDEF): |
|
case (NODE_REFDESCRIPTOR): |
|
case (NODE_REFENTRYTITLE): |
|
case (NODE_REFNAME): |
|
case (NODE_REFPURPOSE): |
|
case (NODE_SYNOPSIS): |
|
case (NODE_TITLE): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
return(0); |
|
case (NODE_PROGRAMLISTING): |
|
switch (parent) { |
|
case (NODE_PARA): |
|
case (NODE_REFSECT1): |
|
case (NODE_REFSYNOPSISDIV): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
return(0); |
|
case (NODE_REFCLASS): |
|
return(parent == NODE_REFNAMEDIV); |
|
case (NODE_REFDESCRIPTOR): |
|
return(parent == NODE_REFNAMEDIV); |
|
case (NODE_REFENTRY): |
|
return(parent == NODE_ROOT); |
|
case (NODE_REFENTRYTITLE): |
|
switch (parent) { |
|
case (NODE_CITEREFENTRY): |
|
case (NODE_REFMETA): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
case (NODE_REFMETA): |
|
return(parent == NODE_REFENTRY); |
|
case (NODE_REFMISCINFO): |
|
return(parent == NODE_REFMETA); |
|
case (NODE_REFNAME): |
|
return(parent == NODE_REFNAMEDIV); |
|
case (NODE_REFNAMEDIV): |
|
return(parent == NODE_REFENTRY); |
|
case (NODE_REFPURPOSE): |
|
return(parent == NODE_REFNAMEDIV); |
|
case (NODE_REFSECT1): |
|
return(parent == NODE_REFENTRY); |
|
case (NODE_REFSYNOPSISDIV): |
|
return(parent == NODE_REFENTRY); |
|
case (NODE_SYNOPSIS): |
|
switch (parent) { |
|
case (NODE_REFSYNOPSISDIV): |
|
case (NODE_REFSECT1): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
return(0); |
|
case (NODE_TITLE): |
|
switch (parent) { |
|
case (NODE_REFSECT1): |
|
case (NODE_REFSYNOPSISDIV): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
return(0); |
|
case (NODE_TEXT): |
|
return(1); |
|
case (NODE__MAX): |
|
break; |
break; |
|
case LINE_MACRO: |
|
if (accept_arg) |
|
putchar(' '); |
|
else |
|
macro_close(f); |
|
break; |
} |
} |
|
|
abort(); |
if (n->node == NODE_ESCAPE) { |
return(0); |
fputs(n->b, stdout); |
|
if (f->linestate == LINE_NEW) |
|
f->linestate = LINE_TEXT; |
|
return; |
|
} |
|
|
|
/* |
|
* Remove the prefix '-' from <option> elements |
|
* because the arguments of .Fl macros do not need it. |
|
*/ |
|
|
|
if (n->parent != NULL && n->parent->node == NODE_OPTION && *cp == '-') |
|
cp++; |
|
|
|
if (f->linestate == LINE_MACRO) |
|
macro_addarg(f, cp, 0); |
|
else |
|
print_text(f, cp, 0); |
} |
} |
|
|
static void |
static void |
xml_char(void *arg, const XML_Char *p, int sz) |
pnode_printpara(struct format *f, struct pnode *n) |
{ |
{ |
struct parse *ps = arg; |
struct pnode *np; |
struct pnode *dat; |
|
|
|
/* Stopped or no tree yet. */ |
if (n->parent == NULL) |
if (ps->stop || NODE_ROOT == ps->node) |
|
return; |
return; |
|
|
/* Not supposed to be collecting text. */ |
if ((np = TAILQ_PREV(n, pnodeq, child)) == NULL) |
assert(NULL != ps->cur); |
np = n->parent; |
if (NODE_IGNTEXT & nodes[ps->node].flags) |
|
return; |
|
|
|
/* |
f->flags = 0; |
* Are we in the midst of processing text? |
|
* If we're not processing text right now, then create a text |
|
* node for doing so. |
|
*/ |
|
if (NODE_TEXT != ps->node) { |
|
dat = calloc(1, sizeof(struct pnode)); |
|
if (NULL == dat) { |
|
perror(NULL); |
|
exit(EXIT_FAILURE); |
|
} |
|
|
|
dat->node = ps->node = NODE_TEXT; |
switch (np->node) { |
dat->parent = ps->cur; |
case NODE_ENTRY: |
TAILQ_INIT(&dat->childq); |
case NODE_GLOSSTERM: |
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
case NODE_LISTITEM: |
ps->cur = dat; |
case NODE_TERM: |
assert(NULL != ps->root); |
return; |
|
case NODE_APPENDIX: |
|
case NODE_LEGALNOTICE: |
|
case NODE_PREFACE: |
|
case NODE_SECTION: |
|
if (f->level < 3) |
|
return; |
|
break; |
|
default: |
|
break; |
} |
} |
|
macro_line(f, "Pp"); |
|
} |
|
|
/* Append to current buffer. */ |
static void |
assert(sz >= 0); |
pnode_printrefnamediv(struct format *f, struct pnode *n) |
ps->cur->b = realloc(ps->cur->b, |
{ |
ps->cur->bsz + (size_t)sz); |
struct pnode *nc, *nn; |
if (NULL == ps->cur->b) { |
int comma; |
perror(NULL); |
|
exit(EXIT_FAILURE); |
macro_line(f, "Sh NAME"); |
|
comma = 0; |
|
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
|
if (nc->node != NODE_REFNAME) |
|
continue; |
|
if (comma) |
|
macro_addarg(f, ",", ARG_SPACE); |
|
macro_open(f, "Nm"); |
|
macro_addnode(f, nc, ARG_SPACE); |
|
pnode_unlink(nc); |
|
comma = 1; |
} |
} |
memcpy(ps->cur->b + ps->cur->bsz, p, sz); |
macro_close(f); |
ps->cur->bsz += (size_t)sz; |
|
} |
} |
|
|
/* |
/* |
* Begin an element. |
* If the SYNOPSIS macro has a superfluous title, kill it. |
* 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're at the root and already have a tree, puke and exit. |
|
* Make sure that the element is in the right context. |
|
* Lastly, put the node onto our parse tree and continue. |
|
*/ |
*/ |
static void |
static void |
xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts) |
pnode_printrefsynopsisdiv(struct format *f, struct pnode *n) |
{ |
{ |
struct parse *ps = arg; |
struct pnode *nc, *nn; |
enum nodeid node; |
|
struct pnode *dat; |
|
|
|
if (ps->stop) |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) |
|
if (nc->node == NODE_TITLE) |
|
pnode_unlink(nc); |
|
|
|
macro_line(f, "Sh SYNOPSIS"); |
|
} |
|
|
|
/* |
|
* Start a hopefully-named `Sh' section. |
|
*/ |
|
static void |
|
pnode_printrefsect(struct format *f, struct pnode *n) |
|
{ |
|
struct pnode *nc, *ncc; |
|
const char *title; |
|
int flags, level; |
|
|
|
if (n->parent == NULL) |
return; |
return; |
|
|
/* Close out text node, if applicable... */ |
level = ++f->level; |
if (NODE_TEXT == ps->node) { |
flags = ARG_SPACE; |
assert(NULL != ps->cur); |
if (level == 1) |
ps->cur = ps->cur->parent; |
flags |= ARG_UPPER; |
assert(NULL != ps->cur); |
if (level < 3) { |
ps->node = ps->cur->node; |
switch (n->node) { |
|
case NODE_CAUTION: |
|
case NODE_NOTE: |
|
case NODE_TIP: |
|
case NODE_WARNING: |
|
level = 3; |
|
break; |
|
default: |
|
break; |
|
} |
} |
} |
|
|
for (node = 0; node < NODE__MAX; node++) |
TAILQ_FOREACH(nc, &n->childq, child) |
if (NULL == nodes[node].name) |
if (nc->node == NODE_TITLE) |
continue; |
|
else if (0 == strcmp(nodes[node].name, name)) |
|
break; |
break; |
|
|
if (NODE__MAX == node && NODE_ROOT == ps->node) { |
if (nc == NULL) { |
fprintf(stderr, "%s: ignoring node\n", name); |
switch (n->node) { |
return; |
case NODE_PREFACE: |
} else if (NODE__MAX == node) { |
title = "Preface"; |
fprintf(stderr, "%s: unknown node\n", name); |
break; |
ps->stop = 1; |
case NODE_APPENDIX: |
return; |
title = "Appendix"; |
} else if (NODE_ROOT == ps->node && NULL != ps->root) { |
break; |
fprintf(stderr, "%s: reentering?\n", name); |
case NODE_LEGALNOTICE: |
ps->stop = 1; |
title = "Legal Notice"; |
return; |
break; |
} else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) { |
case NODE_CAUTION: |
fprintf(stderr, "%s: known node w/o context\n", name); |
title = "Caution"; |
return; |
break; |
} else if ( ! isparent(node, ps->node)) { |
case NODE_NOTE: |
fprintf(stderr, "%s: bad parent\n", name); |
title = "Note"; |
ps->stop = 1; |
break; |
return; |
case NODE_TIP: |
|
title = "Tip"; |
|
break; |
|
case NODE_WARNING: |
|
title = "Warning"; |
|
break; |
|
default: |
|
title = "Unknown"; |
|
break; |
|
} |
} |
} |
|
|
if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { |
switch (level) { |
perror(NULL); |
case 1: |
exit(EXIT_FAILURE); |
macro_close(f); |
|
macro_open(f, "Sh"); |
|
break; |
|
case 2: |
|
macro_close(f); |
|
macro_open(f, "Ss"); |
|
break; |
|
default: |
|
pnode_printpara(f, n); |
|
macro_open(f, "Sy"); |
|
break; |
} |
} |
|
|
dat->node = ps->node = node; |
if (nc != NULL) |
dat->parent = ps->cur; |
macro_addnode(f, nc, flags); |
TAILQ_INIT(&dat->childq); |
else |
|
macro_addarg(f, title, flags | ARG_QUOTED); |
|
macro_close(f); |
|
|
if (NULL != ps->cur) |
/* |
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
* DocBook has no equivalent for -split mode, |
|
* so just switch the default in the AUTHORS section. |
|
*/ |
|
|
ps->cur = dat; |
if (nc != NULL) { |
if (NULL == ps->root) |
ncc = TAILQ_FIRST(&nc->childq); |
ps->root = dat; |
if (ncc != NULL && ncc->node == NODE_TEXT && |
|
strcasecmp(ncc->b, "AUTHORS") == 0) |
|
macro_line(f, "An -nosplit"); |
|
pnode_unlink(nc); |
|
} |
} |
} |
|
|
/* |
/* |
* Roll up the parse tree. |
* Start a reference, extracting the title and volume. |
* Does nothing else special. |
|
* If we hit the root, then assign ourselves as the NODE_ROOT. |
|
*/ |
*/ |
static void |
static void |
xml_elem_end(void *arg, const XML_Char *name) |
pnode_printciterefentry(struct format *f, struct pnode *n) |
{ |
{ |
struct parse *ps = arg; |
struct pnode *nc, *title, *manvol; |
|
|
if (ps->stop || NODE_ROOT == ps->node) |
title = manvol = NULL; |
return; |
TAILQ_FOREACH(nc, &n->childq, child) { |
|
if (nc->node == NODE_MANVOLNUM) |
/* Close out text node, if applicable... */ |
manvol = nc; |
if (NODE_TEXT == ps->node) { |
else if (nc->node == NODE_REFENTRYTITLE) |
assert(NULL != ps->cur); |
title = nc; |
ps->cur = ps->cur->parent; |
|
assert(NULL != ps->cur); |
|
ps->node = ps->cur->node; |
|
} |
} |
|
macro_open(f, "Xr"); |
if (NULL == (ps->cur = ps->cur->parent)) |
if (title == NULL) |
ps->node = NODE_ROOT; |
macro_addarg(f, "unknown", ARG_SPACE); |
else |
else |
ps->node = ps->cur->node; |
macro_addnode(f, title, ARG_SPACE | ARG_SINGLE); |
|
if (manvol == NULL) |
|
macro_addarg(f, "1", ARG_SPACE); |
|
else |
|
macro_addnode(f, manvol, ARG_SPACE | ARG_SINGLE); |
|
pnode_unlinksub(n); |
} |
} |
|
|
static void |
static void |
pnode_free(struct pnode *pn) |
pnode_printfuncdef(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc; |
|
|
if (NULL == pn) |
nc = TAILQ_FIRST(&n->childq); |
return; |
if (nc != NULL && nc->node == NODE_TEXT) { |
|
macro_argline(f, "Ft", nc->b); |
while (NULL != (pp = TAILQ_FIRST(&pn->childq))) { |
pnode_unlink(nc); |
TAILQ_REMOVE(&pn->childq, pp, child); |
|
pnode_free(pp); |
|
} |
} |
|
macro_nodeline(f, "Fo", n, ARG_SINGLE); |
free(pn->b); |
pnode_unlinksub(n); |
free(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 |
static void |
pnode_unlink(struct pnode *pn) |
pnode_printmathfenced(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *nc; |
|
|
if (NULL != pn->parent) |
printf("left %s ", pnode_getattr_raw(n, ATTRKEY_OPEN, "(")); |
TAILQ_REMOVE(&pn->parent->childq, pn, child); |
|
pnode_free(pn); |
|
} |
|
|
|
static void |
nc = TAILQ_FIRST(&n->childq); |
bufclear(struct parse *p) |
pnode_print(f, nc); |
{ |
|
|
|
p->b[p->bsz = 0] = '\0'; |
while ((nc = TAILQ_NEXT(nc, child)) != NULL) { |
|
putchar(','); |
|
pnode_print(f, nc); |
|
} |
|
printf("right %s ", pnode_getattr_raw(n, ATTRKEY_CLOSE, ")")); |
|
pnode_unlinksub(n); |
} |
} |
|
|
|
/* |
|
* These math nodes require special handling because they have infix |
|
* syntax, instead of the usual prefix or prefix. |
|
* So we need to break up the first and second child node with a |
|
* particular eqn(7) word. |
|
*/ |
static void |
static void |
bufappend(struct parse *p, struct pnode *pn) |
pnode_printmath(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *nc; |
|
|
assert(NODE_TEXT == pn->node); |
nc = TAILQ_FIRST(&n->childq); |
if (p->bsz + pn->bsz + 1 > p->mbsz) { |
pnode_print(f, nc); |
p->mbsz = p->bsz + pn->bsz + 1; |
|
if (NULL == (p->b = realloc(p->b, p->mbsz))) { |
switch (n->node) { |
perror(NULL); |
case NODE_MML_MSUP: |
exit(EXIT_FAILURE); |
fputs(" sup ", stdout); |
} |
break; |
|
case NODE_MML_MFRAC: |
|
fputs(" over ", stdout); |
|
break; |
|
case NODE_MML_MSUB: |
|
fputs(" sub ", stdout); |
|
break; |
|
default: |
|
break; |
} |
} |
memcpy(p->b + p->bsz, pn->b, pn->bsz); |
|
p->bsz += pn->bsz; |
nc = TAILQ_NEXT(nc, child); |
p->b[p->bsz] = '\0'; |
pnode_print(f, nc); |
|
pnode_unlinksub(n); |
} |
} |
|
|
static void |
static void |
bufappend_r(struct parse *p, struct pnode *pn) |
pnode_printfuncprototype(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc, *fdef; |
|
|
if (NODE_TEXT == pn->node) |
TAILQ_FOREACH(fdef, &n->childq, child) |
bufappend(p, pn); |
if (fdef->node == NODE_FUNCDEF) |
TAILQ_FOREACH(pp, &pn->childq, child) |
break; |
bufappend_r(p, pp); |
|
|
if (fdef != NULL) { |
|
pnode_printfuncdef(f, fdef); |
|
pnode_unlink(fdef); |
|
} else |
|
macro_line(f, "Fo UNKNOWN"); |
|
|
|
TAILQ_FOREACH(nc, &n->childq, child) |
|
macro_nodeline(f, "Fa", nc, ARG_SINGLE); |
|
|
|
macro_line(f, "Fc"); |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
/* |
* Print text presumably on a macro line. |
* The <arg> element is more complicated than it should be because text |
* Ignore any child macros. |
* nodes are treated like ".Ar foo", but non-text nodes need to be |
* Convert all whitespace to regular spaces. |
* re-sent into the printer (i.e., without the preceding ".Ar"). |
|
* This also handles the case of "repetition" (or in other words, the |
|
* ellipsis following an argument) and optionality. |
*/ |
*/ |
static void |
static void |
pnode_printmacrolinepart(struct parse *p, struct pnode *pn) |
pnode_printarg(struct format *f, struct pnode *n) |
{ |
{ |
char *cp; |
struct pnode *nc; |
|
struct pattr *a; |
|
int isop, isrep, was_impl; |
|
|
bufclear(p); |
isop = 1; |
bufappend_r(p, pn); |
isrep = was_impl = 0; |
|
TAILQ_FOREACH(a, &n->attrq, child) { |
|
if (a->key == ATTRKEY_CHOICE && |
|
(a->val == ATTRVAL_PLAIN || a->val == ATTRVAL_REQ)) |
|
isop = 0; |
|
else if (a->key == ATTRKEY_REP && a->val == ATTRVAL_REPEAT) |
|
isrep = 1; |
|
} |
|
if (isop) { |
|
if (f->flags & FMT_IMPL) { |
|
was_impl = 1; |
|
macro_open(f, "Oo"); |
|
} else { |
|
macro_open(f, "Op"); |
|
f->flags |= FMT_IMPL; |
|
} |
|
} |
|
|
/* Convert all space to spaces. */ |
TAILQ_FOREACH(nc, &n->childq, child) { |
for (cp = p->b; '\0' != *cp; cp++) |
if (nc->node == NODE_TEXT) |
if (isspace((int)*cp)) |
macro_open(f, "Ar"); |
*cp = ' '; |
pnode_print(f, nc); |
|
if (isrep && nc->node == NODE_TEXT) |
for (cp = p->b; isspace((int)*cp); cp++) |
macro_addarg(f, "...", ARG_SPACE); |
/* Spin. */ ; |
|
|
|
for ( ; '\0' != *cp; cp++) { |
|
/* Escape us if we look like a macro. */ |
|
if ((cp == p->b || ' ' == *(cp - 1)) && |
|
isupper((int)*cp) && |
|
'\0' != *(cp + 1) && |
|
islower((int)*(cp + 1)) && |
|
('\0' == *(cp + 2) || |
|
' ' == *(cp + 2) || |
|
(islower((int)*(cp + 2)) && |
|
('\0' == *(cp + 3) || |
|
' ' == *(cp + 3))))) |
|
fputs("\\&", stdout); |
|
putchar(*cp); |
|
/* If we're a character escape, escape us. */ |
|
if ('\\' == *cp) |
|
putchar('e'); |
|
} |
} |
|
if (isop) { |
|
if (was_impl) |
|
macro_open(f, "Oc"); |
|
else |
|
f->flags &= ~FMT_IMPL; |
|
} |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
|
* Just pnode_printmacrolinepart() but with a newline. |
|
* If no text, just the newline. |
|
*/ |
|
static void |
static void |
pnode_printmacroline(struct parse *p, struct pnode *pn) |
pnode_printgroup(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *nc, *nn; |
|
struct pattr *a; |
|
int isop, sv; |
|
|
pnode_printmacrolinepart(p, pn); |
isop = 1; |
putchar('\n'); |
TAILQ_FOREACH(a, &n->attrq, child) |
|
if (a->key == ATTRKEY_CHOICE && |
|
(a->val == ATTRVAL_PLAIN || a->val == ATTRVAL_REQ)) { |
|
isop = 0; |
|
break; |
|
} |
|
|
|
/* |
|
* Make sure we're on a macro line. |
|
* This will prevent pnode_print() for putting us on a |
|
* subsequent line. |
|
*/ |
|
sv = f->linestate == LINE_NEW; |
|
if (isop) |
|
macro_open(f, "Op"); |
|
else if (sv) |
|
macro_open(f, "No"); |
|
f->flags |= FMT_IMPL; |
|
|
|
/* |
|
* Keep on printing text separated by the vertical bar as long |
|
* as we're within the same origin node as the group. |
|
* This is kind of a nightmare. |
|
* Eh, DocBook... |
|
* FIXME: if there's a "Fl", we don't cut off the leading "-" |
|
* like we do in pnode_print(). |
|
*/ |
|
TAILQ_FOREACH(nc, &n->childq, child) { |
|
pnode_print(f, nc); |
|
nn = TAILQ_NEXT(nc, child); |
|
while (nn != NULL) { |
|
if (nc->node != nn->node) |
|
break; |
|
macro_addarg(f, "|", ARG_SPACE); |
|
macro_addnode(f, nn, ARG_SPACE); |
|
nc = nn; |
|
nn = TAILQ_NEXT(nn, child); |
|
} |
|
} |
|
if (sv) |
|
macro_close(f); |
|
f->flags &= ~FMT_IMPL; |
|
pnode_unlinksub(n); |
} |
} |
|
|
static void |
static void |
pnode_printrefsect(struct parse *p, struct pnode *pn) |
pnode_printauthor(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc, *nn; |
|
int have_contrib, have_name; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
/* |
if (NODE_TITLE == pp->node) |
* Print <contrib> children up front, before the .An scope, |
|
* and figure out whether we a name of a person. |
|
*/ |
|
|
|
have_contrib = have_name = 0; |
|
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
|
switch (nc->node) { |
|
case NODE_CONTRIB: |
|
if (have_contrib) |
|
print_text(f, ",", 0); |
|
print_textnode(f, nc); |
|
pnode_unlink(nc); |
|
have_contrib = 1; |
break; |
break; |
|
case NODE_PERSONNAME: |
|
have_name = 1; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
if (TAILQ_FIRST(&n->childq) == NULL) |
|
return; |
|
|
if (NULL != pp) { |
if (have_contrib) |
fputs(".Sh ", stdout); |
print_text(f, ":", 0); |
pnode_printmacroline(p, pp); |
|
pnode_unlink(pp); |
/* |
} else |
* If we have a name, print it in the .An scope and leave |
puts(".Sh UNKNOWN"); |
* all other content for child handlers, to print after the |
|
* scope. Otherwise, print everything in the scope. |
|
*/ |
|
|
|
macro_open(f, "An"); |
|
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
|
if (nc->node == NODE_PERSONNAME || have_name == 0) { |
|
macro_addnode(f, nc, ARG_SPACE); |
|
pnode_unlink(nc); |
|
} |
|
} |
|
|
|
/* |
|
* If there is an email address, |
|
* print it on the same macro line. |
|
*/ |
|
|
|
if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) { |
|
f->flags |= FMT_CHILD; |
|
macro_open(f, "Aq Mt"); |
|
macro_addnode(f, nc, ARG_SPACE); |
|
pnode_unlink(nc); |
|
} |
|
|
|
/* |
|
* If there are still unprinted children, end the scope |
|
* with a comma. Otherwise, leave the scope open in case |
|
* a text node follows that starts with closing punctuation. |
|
*/ |
|
|
|
if (TAILQ_FIRST(&n->childq) != NULL) { |
|
macro_addarg(f, ",", ARG_SPACE); |
|
macro_close(f); |
|
} |
} |
} |
|
|
static void |
static void |
pnode_printciterefentry(struct parse *p, struct pnode *pn) |
pnode_printlink(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *title, *manvol; |
struct pnode *nc; |
|
const char *uri, *text; |
|
|
title = manvol = NULL; |
uri = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL); |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (uri != NULL) { |
if (NODE_MANVOLNUM == pp->node) |
if (TAILQ_FIRST(&n->childq) != NULL) { |
manvol = pp; |
TAILQ_FOREACH(nc, &n->childq, child) |
else if (NODE_REFENTRYTITLE == pp->node) |
pnode_print(f, nc); |
title = pp; |
text = ""; |
|
} else if ((text = pnode_getattr_raw(n, |
fputs(".Xr ", stdout); |
ATTRKEY_ENDTERM, NULL)) != NULL) { |
if (NULL != title) { |
if (f->linestate == LINE_MACRO && f->flags & FMT_ARG) |
pnode_printmacrolinepart(p, title); |
macro_addarg(f, text, ARG_SPACE); |
pnode_unlink(title); |
else |
} else |
print_text(f, text, ARG_SPACE); |
fputs("unknown", stdout); |
} |
putchar(' '); |
if (text != NULL) { |
if (NULL != manvol) { |
if (f->flags & FMT_IMPL) |
pnode_printmacroline(p, manvol); |
macro_open(f, "Po"); |
pnode_unlink(manvol); |
else { |
} else |
macro_open(f, "Pq"); |
puts("1"); |
f->flags |= FMT_CHILD; |
|
} |
|
} |
|
macro_open(f, "Sx"); |
|
macro_addarg(f, uri, ARG_SPACE); |
|
if (text != NULL && f->flags & FMT_IMPL) |
|
macro_open(f, "Pc"); |
|
pnode_unlinksub(n); |
|
return; |
|
} |
|
uri = pnode_getattr_raw(n, ATTRKEY_XLINK_HREF, NULL); |
|
if (uri == NULL) |
|
uri = pnode_getattr_raw(n, ATTRKEY_URL, NULL); |
|
if (uri != NULL) { |
|
macro_open(f, "Lk"); |
|
macro_addarg(f, uri, ARG_SPACE | ARG_SINGLE); |
|
if (TAILQ_FIRST(&n->childq) != NULL) |
|
macro_addnode(f, n, ARG_SPACE | ARG_SINGLE); |
|
pnode_unlinksub(n); |
|
return; |
|
} |
} |
} |
|
|
static void |
static void |
pnode_printrefmeta(struct parse *p, struct pnode *pn) |
pnode_printprologue(struct format *f, struct pnode *root) |
{ |
{ |
struct pnode *pp, *title, *manvol; |
struct pnode *date, *refmeta, *name, *vol, *descr, *nc; |
|
const char *sname; |
|
|
title = manvol = NULL; |
/* Collect information. */ |
TAILQ_FOREACH(pp, &pn->childq, child) |
|
if (NODE_MANVOLNUM == pp->node) |
|
manvol = pp; |
|
else if (NODE_REFENTRYTITLE == pp->node) |
|
title = pp; |
|
|
|
puts(".Dd $Mdocdate" "$"); |
if ((date = pnode_takefirst(root, NODE_PUBDATE)) == NULL) |
fputs(".Dt ", stdout); |
date = pnode_takefirst(root, NODE_DATE); |
|
|
if (NULL != title) { |
name = vol = NULL; |
pnode_printmacrolinepart(p, title); |
if ((refmeta = pnode_takefirst(root, NODE_REFMETA)) != NULL) { |
pnode_unlink(title); |
TAILQ_FOREACH(nc, &refmeta->childq, child) { |
|
switch (nc->node) { |
|
case NODE_REFENTRYTITLE: |
|
name = nc; |
|
break; |
|
case NODE_MANVOLNUM: |
|
vol = nc; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
} |
|
|
|
if (pnode_findfirst(root, NODE_REFNAMEDIV) == NULL && |
|
((nc = pnode_findfirst(root, NODE_BOOKINFO)) != NULL || |
|
(nc = pnode_findfirst(root, NODE_REFENTRYINFO)) != NULL)) |
|
descr = pnode_takefirst(nc, NODE_TITLE); |
|
else |
|
descr = NULL; |
|
|
|
/* Print prologue. */ |
|
|
|
if (date == NULL) |
|
macro_line(f, "Dd $Mdocdate" "$"); |
|
else |
|
macro_nodeline(f, "Dd", date, 0); |
|
|
|
macro_open(f, "Dt"); |
|
if (name == NULL) { |
|
sname = pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN"); |
|
macro_addarg(f, sname, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
} else |
} else |
fputs("UNKNOWN", stdout); |
macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
putchar(' '); |
if (vol == NULL) |
if (NULL != manvol) { |
macro_addarg(f, "1", ARG_SPACE); |
pnode_printmacroline(p, manvol); |
else |
pnode_unlink(manvol); |
macro_addnode(f, vol, ARG_SPACE | ARG_SINGLE); |
} else |
|
puts("1"); |
|
|
|
puts(".Os"); |
macro_line(f, "Os"); |
|
|
|
if (descr != NULL) { |
|
macro_line(f, "Sh NAME"); |
|
if (name == NULL) |
|
macro_argline(f, "Nm", sname); |
|
else |
|
macro_nodeline(f, "Nm", name, ARG_SINGLE); |
|
macro_nodeline(f, "Nd", descr, 0); |
|
} |
|
|
|
/* Clean up. */ |
|
|
|
pnode_unlink(date); |
|
pnode_unlink(refmeta); |
|
pnode_unlink(descr); |
} |
} |
|
|
|
/* |
|
* We can have multiple <term> elements within a <varlistentry>, which |
|
* we should comma-separate as list headers. |
|
*/ |
static void |
static void |
pnode_printfuncdef(struct parse *p, struct pnode *pn) |
pnode_printvarlistentry(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *ftype, *func; |
struct pnode *nc, *nn; |
|
int first = 1; |
|
|
ftype = func = NULL; |
macro_open(f, "It"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
f->flags |= FMT_IMPL; |
if (NODE_TEXT == pp->node) |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
ftype = pp; |
if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM) |
else if (NODE_FUNCTION == pp->node) |
continue; |
func = pp; |
if (first == 0) { |
|
switch (f->linestate) { |
if (NULL != ftype) { |
case LINE_NEW: |
fputs(".Ft ", stdout); |
break; |
pnode_printmacroline(p, ftype); |
case LINE_TEXT: |
|
print_text(f, ",", 0); |
|
break; |
|
case LINE_MACRO: |
|
macro_addarg(f, ",", 0); |
|
break; |
|
} |
|
} |
|
pnode_print(f, nc); |
|
pnode_unlink(nc); |
|
first = 0; |
} |
} |
|
macro_close(f); |
if (NULL != func) { |
while ((nc = TAILQ_FIRST(&n->childq)) != NULL) { |
fputs(".Fo ", stdout); |
pnode_print(f, nc); |
pnode_printmacroline(p, func); |
pnode_unlink(nc); |
} else |
} |
puts(".Fo UNKNOWN"); |
macro_close(f); |
} |
} |
|
|
static void |
static void |
pnode_printparamdef(struct parse *p, struct pnode *pn) |
pnode_printtitle(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *ptype, *param; |
struct pnode *nc, *nn; |
|
|
ptype = param = NULL; |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
TAILQ_FOREACH(pp, &pn->childq, child) |
if (nc->node == NODE_TITLE) { |
if (NODE_TEXT == pp->node) |
pnode_printpara(f, nc); |
ptype = pp; |
pnode_print(f, nc); |
else if (NODE_PARAMETER == pp->node) |
pnode_unlink(nc); |
param = pp; |
} |
|
} |
|
} |
|
|
fputs(".Fa \"", stdout); |
static void |
if (NULL != ptype) { |
pnode_printrow(struct format *f, struct pnode *n) |
pnode_printmacrolinepart(p, ptype); |
{ |
putchar(' '); |
struct pnode *nc; |
|
|
|
macro_line(f, "Bl -dash -compact"); |
|
TAILQ_FOREACH(nc, &n->childq, child) { |
|
macro_line(f, "It"); |
|
pnode_print(f, nc); |
} |
} |
|
macro_line(f, "El"); |
|
pnode_unlink(n); |
|
} |
|
|
if (NULL != param) |
static void |
pnode_printmacrolinepart(p, param); |
pnode_printtgroup1(struct format *f, struct pnode *n) |
else |
{ |
fputs("UNKNOWN", stdout); |
struct pnode *nc; |
|
|
puts("\""); |
macro_line(f, "Bl -bullet -compact"); |
|
while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) { |
|
macro_line(f, "It"); |
|
pnode_print(f, nc); |
|
pnode_unlink(nc); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
} |
} |
|
|
static void |
static void |
pnode_printfuncprototype(struct parse *p, struct pnode *pn) |
pnode_printtgroup2(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *fdef; |
struct pnode *nr, *ne; |
|
|
TAILQ_FOREACH(fdef, &pn->childq, child) |
macro_line(f, "Bl -tag -width Ds"); |
if (NODE_FUNCDEF == fdef->node) |
while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) { |
|
if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL) |
break; |
break; |
|
macro_open(f, "It"); |
|
f->flags |= FMT_IMPL; |
|
pnode_print(f, ne); |
|
macro_close(f); |
|
pnode_unlink(ne); |
|
pnode_print(f, nr); |
|
pnode_unlink(nr); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
if (NULL != fdef) { |
static void |
pnode_printfuncdef(p, fdef); |
pnode_printtgroup(struct format *f, struct pnode *n) |
pnode_unlink(fdef); |
{ |
} else |
struct pnode *nc; |
puts(".Fo UNKNOWN"); |
|
|
|
TAILQ_FOREACH(pp, &pn->childq, child) { |
switch (atoi(pnode_getattr_raw(n, ATTRKEY_COLS, "0"))) { |
if (NODE_PARAMDEF == pp->node) |
case 1: |
pnode_printparamdef(p, pp); |
pnode_printtgroup1(f, n); |
pnode_unlink(pp); |
return; |
|
case 2: |
|
pnode_printtgroup2(f, n); |
|
return; |
|
default: |
|
break; |
} |
} |
|
|
puts(".Fc"); |
macro_line(f, "Bl -ohang"); |
|
while ((nc = pnode_findfirst(n, NODE_ROW)) != NULL) { |
|
macro_line(f, "It Table Row"); |
|
pnode_printrow(f, nc); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
} |
} |
|
|
|
static void |
|
pnode_printlist(struct format *f, struct pnode *n) |
|
{ |
|
struct pnode *nc; |
|
|
|
pnode_printtitle(f, n); |
|
macro_argline(f, "Bl", |
|
n->node == NODE_ORDEREDLIST ? "-enum" : "-bullet"); |
|
TAILQ_FOREACH(nc, &n->childq, child) { |
|
macro_line(f, "It"); |
|
pnode_print(f, nc); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
|
static void |
|
pnode_printvariablelist(struct format *f, struct pnode *n) |
|
{ |
|
struct pnode *nc; |
|
|
|
pnode_printtitle(f, n); |
|
macro_line(f, "Bl -tag -width Ds"); |
|
TAILQ_FOREACH(nc, &n->childq, child) { |
|
if (nc->node == NODE_VARLISTENTRY) |
|
pnode_printvarlistentry(f, nc); |
|
else |
|
macro_nodeline(f, "It", nc, 0); |
|
} |
|
macro_line(f, "El"); |
|
pnode_unlinksub(n); |
|
} |
|
|
/* |
/* |
* Print a parsed node (or ignore it--whatever). |
* Print a parsed node (or ignore it--whatever). |
* This is a recursive function. |
* This is a recursive function. |
* FIXME: macro line continuation? |
* FIXME: if we're in a literal context (<screen> or <programlisting> or |
|
* whatever), don't print inline macros. |
*/ |
*/ |
static void |
static void |
pnode_print(struct parse *p, struct pnode *pn) |
pnode_print(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc, *nn; |
char *cp; |
int was_impl; |
int last; |
|
|
|
if (NULL == pn) |
if (n == NULL) |
return; |
return; |
|
|
if (NODE_TEXT != pn->node && NODE_ROOT != pn->node) |
was_impl = f->flags & FMT_IMPL; |
printf(".\\\" %s\n", nodes[pn->node].name); |
if (n->spc) |
|
f->flags &= ~FMT_NOSPC; |
|
else |
|
f->flags |= FMT_NOSPC; |
|
|
switch (pn->node) { |
switch (n->node) { |
case (NODE_CITEREFENTRY): |
case NODE_APPLICATION: |
pnode_printciterefentry(p, pn); |
macro_open(f, "Nm"); |
break; |
break; |
case (NODE_CODE): |
case NODE_ARG: |
fputs(".Li ", stdout); |
pnode_printarg(f, n); |
pnode_printmacroline(p, pn); |
|
break; |
break; |
case (NODE_FUNCTION): |
case NODE_AUTHOR: |
fputs(".Fn ", stdout); |
pnode_printauthor(f, n); |
pnode_printmacroline(p, pn); |
|
break; |
break; |
case (NODE_FUNCPROTOTYPE): |
case NODE_AUTHORGROUP: |
pnode_printfuncprototype(p, pn); |
macro_line(f, "An -split"); |
break; |
break; |
case (NODE_FUNCSYNOPSISINFO): |
case NODE_BLOCKQUOTE: |
fputs(".Fd ", stdout); |
macro_line(f, "Bd -ragged -offset indent"); |
pnode_printmacroline(p, pn); |
|
break; |
break; |
case (NODE_PARA): |
case NODE_CITEREFENTRY: |
/* FIXME: not always. */ |
pnode_printciterefentry(f, n); |
puts(".Pp"); |
|
break; |
break; |
case (NODE_PARAMETER): |
case NODE_CITETITLE: |
fputs(".Fa \"", stdout); |
macro_open(f, "%T"); |
pnode_printmacrolinepart(p, pn); |
|
puts("\""); |
|
break; |
break; |
case (NODE_PROGRAMLISTING): |
case NODE_COMMAND: |
puts(".Bd -literal"); |
macro_open(f, "Nm"); |
break; |
break; |
case (NODE_REFMETA): |
case NODE_CONSTANT: |
pnode_printrefmeta(p, pn); |
macro_open(f, "Dv"); |
break; |
break; |
case (NODE_REFNAME): |
case NODE_EDITOR: |
fputs(".Nm ", stdout); |
print_text(f, "editor:", ARG_SPACE); |
pnode_printmacroline(p, pn); |
macro_open(f, "An"); |
return; |
|
case (NODE_REFNAMEDIV): |
|
puts(".Sh NAME"); |
|
break; |
break; |
case (NODE_REFPURPOSE): |
case NODE_EMAIL: |
fputs(".Nd ", stdout); |
if (was_impl) |
pnode_printmacroline(p, pn); |
macro_open(f, "Ao Mt"); |
return; |
else { |
case (NODE_REFSYNOPSISDIV): |
macro_open(f, "Aq Mt"); |
puts(".Sh SYNOPSIS"); |
f->flags |= FMT_IMPL; |
|
} |
break; |
break; |
case (NODE_REFSECT1): |
case NODE_EMPHASIS: |
pnode_printrefsect(p, pn); |
case NODE_FIRSTTERM: |
|
case NODE_GLOSSTERM: |
|
macro_open(f, "Em"); |
break; |
break; |
case (NODE_TEXT): |
case NODE_ENVAR: |
bufclear(p); |
macro_open(f, "Ev"); |
bufappend(p, pn); |
break; |
/* |
case NODE_ERRORNAME: |
* Output all characters, squeezing out whitespace |
macro_open(f, "Er"); |
* between newlines. |
break; |
* XXX: all whitespace, including tabs (?). |
case NODE_FILENAME: |
* Remember to escape control characters and escapes. |
macro_open(f, "Pa"); |
*/ |
break; |
for (last = '\n', cp = p->b; '\0' != *cp; ) { |
case NODE_FUNCTION: |
if ('\n' == last) { |
macro_open(f, "Fn"); |
/* Consume all whitespace. */ |
break; |
if (isspace((int)*cp)) { |
case NODE_FUNCPROTOTYPE: |
while (isspace((int)*cp)) |
pnode_printfuncprototype(f, n); |
cp++; |
break; |
continue; |
case NODE_FUNCSYNOPSISINFO: |
} else if ('\'' == *cp || '.' == *cp) |
macro_open(f, "Fd"); |
fputs("\\&", stdout); |
break; |
} |
case NODE_INFORMALEQUATION: |
putchar(last = *cp++); |
macro_line(f, "Bd -ragged -offset indent"); |
/* If we're a character escape, escape us. */ |
/* FALLTHROUGH */ |
if ('\\' == last) |
case NODE_INLINEEQUATION: |
putchar('e'); |
macro_line(f, "EQ"); |
|
break; |
|
case NODE_ITEMIZEDLIST: |
|
pnode_printlist(f, n); |
|
break; |
|
case NODE_GROUP: |
|
pnode_printgroup(f, n); |
|
break; |
|
case NODE_KEYSYM: |
|
macro_open(f, "Sy"); |
|
break; |
|
case NODE_LINK: |
|
pnode_printlink(f, n); |
|
break; |
|
case NODE_LITERAL: |
|
if (was_impl) |
|
macro_open(f, "So"); |
|
else { |
|
macro_open(f, "Ql"); |
|
f->flags |= FMT_IMPL; |
} |
} |
if ('\n' != last) |
|
putchar('\n'); |
|
break; |
break; |
|
case NODE_LITERALLAYOUT: |
|
macro_close(f); |
|
macro_argline(f, "Bd", pnode_getattr(n, ATTRKEY_CLASS) == |
|
ATTRVAL_MONOSPACED ? "-literal" : "-unfilled"); |
|
break; |
|
case NODE_MARKUP: |
|
macro_open(f, "Ic"); |
|
break; |
|
case NODE_MML_MFENCED: |
|
pnode_printmathfenced(f, n); |
|
break; |
|
case NODE_MML_MROW: |
|
case NODE_MML_MI: |
|
case NODE_MML_MN: |
|
case NODE_MML_MO: |
|
if (TAILQ_EMPTY(&n->childq)) |
|
break; |
|
fputs(" { ", stdout); |
|
break; |
|
case NODE_MML_MFRAC: |
|
case NODE_MML_MSUB: |
|
case NODE_MML_MSUP: |
|
pnode_printmath(f, n); |
|
break; |
|
case NODE_OPTION: |
|
macro_open(f, "Fl"); |
|
break; |
|
case NODE_ORDEREDLIST: |
|
pnode_printlist(f, n); |
|
break; |
|
case NODE_PARA: |
|
pnode_printpara(f, n); |
|
break; |
|
case NODE_PARAMDEF: |
|
case NODE_PARAMETER: |
|
/* More often, these appear inside NODE_FUNCPROTOTYPE. */ |
|
macro_open(f, "Fa"); |
|
macro_addnode(f, n, ARG_SPACE | ARG_SINGLE); |
|
pnode_unlinksub(n); |
|
break; |
|
case NODE_QUOTE: |
|
if (was_impl) |
|
macro_open(f, "Do"); |
|
else { |
|
macro_open(f, "Dq"); |
|
f->flags |= FMT_IMPL; |
|
} |
|
break; |
|
case NODE_PROGRAMLISTING: |
|
case NODE_SCREEN: |
|
case NODE_SYNOPSIS: |
|
macro_line(f, "Bd -literal"); |
|
break; |
|
case NODE_REFENTRYINFO: |
|
/* Suppress. */ |
|
pnode_unlinksub(n); |
|
break; |
|
case NODE_REFNAME: |
|
/* More often, these appear inside NODE_REFNAMEDIV. */ |
|
macro_open(f, "Nm"); |
|
break; |
|
case NODE_REFNAMEDIV: |
|
pnode_printrefnamediv(f, n); |
|
break; |
|
case NODE_REFPURPOSE: |
|
macro_open(f, "Nd"); |
|
break; |
|
case NODE_REFSYNOPSISDIV: |
|
pnode_printrefsynopsisdiv(f, n); |
|
break; |
|
case NODE_PREFACE: |
|
case NODE_SECTION: |
|
case NODE_APPENDIX: |
|
case NODE_LEGALNOTICE: |
|
case NODE_NOTE: |
|
case NODE_TIP: |
|
case NODE_CAUTION: |
|
case NODE_WARNING: |
|
pnode_printrefsect(f, n); |
|
break; |
|
case NODE_REPLACEABLE: |
|
macro_open(f, "Ar"); |
|
break; |
|
case NODE_SBR: |
|
macro_line(f, "br"); |
|
break; |
|
case NODE_TEXT: |
|
case NODE_ESCAPE: |
|
pnode_printtext(f, n); |
|
break; |
|
case NODE_TGROUP: |
|
pnode_printtgroup(f, n); |
|
break; |
|
case NODE_TITLE: |
|
pnode_printpara(f, n); |
|
macro_nodeline(f, "Sy", n, 0); |
|
pnode_unlinksub(n); |
|
break; |
|
case NODE_TYPE: |
|
macro_open(f, "Vt"); |
|
break; |
|
case NODE_VARIABLELIST: |
|
pnode_printvariablelist(f, n); |
|
break; |
|
case NODE_VARNAME: |
|
macro_open(f, "Va"); |
|
break; |
default: |
default: |
break; |
break; |
} |
} |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(nc, &n->childq, child) |
pnode_print(p, pp); |
pnode_print(f, nc); |
|
|
switch (pn->node) { |
switch (n->node) { |
case (NODE_PROGRAMLISTING): |
case NODE_EMAIL: |
puts(".Ed"); |
if (was_impl) { |
|
f->flags &= ~FMT_NOSPC; |
|
macro_open(f, "Ac"); |
|
} else |
|
f->flags &= ~FMT_IMPL; |
break; |
break; |
|
case NODE_ESCAPE: |
|
case NODE_TERM: |
|
case NODE_TEXT: |
|
/* Accept more arguments to the previous macro. */ |
|
return; |
|
case NODE_INFORMALEQUATION: |
|
macro_line(f, "EN"); |
|
macro_line(f, "Ed"); |
|
break; |
|
case NODE_INLINEEQUATION: |
|
macro_line(f, "EN"); |
|
break; |
|
case NODE_LITERAL: |
|
if (was_impl) { |
|
f->flags &= ~FMT_NOSPC; |
|
macro_open(f, "Sc"); |
|
} else |
|
f->flags &= ~FMT_IMPL; |
|
break; |
|
case NODE_MEMBER: |
|
if ((nn = TAILQ_NEXT(n, child)) != NULL && |
|
nn->node != NODE_MEMBER) |
|
nn = NULL; |
|
switch (f->linestate) { |
|
case LINE_TEXT: |
|
if (nn != NULL) |
|
print_text(f, ",", 0); |
|
break; |
|
case LINE_MACRO: |
|
if (nn != NULL) |
|
macro_addarg(f, ",", ARG_SPACE); |
|
macro_close(f); |
|
break; |
|
case LINE_NEW: |
|
break; |
|
} |
|
break; |
|
case NODE_MML_MROW: |
|
case NODE_MML_MI: |
|
case NODE_MML_MN: |
|
case NODE_MML_MO: |
|
if (TAILQ_EMPTY(&n->childq)) |
|
break; |
|
fputs(" } ", stdout); |
|
break; |
|
case NODE_QUOTE: |
|
if (was_impl) { |
|
f->flags &= ~FMT_NOSPC; |
|
macro_open(f, "Dc"); |
|
} else |
|
f->flags &= ~FMT_IMPL; |
|
break; |
|
case NODE_PREFACE: |
|
case NODE_SECTION: |
|
case NODE_APPENDIX: |
|
case NODE_LEGALNOTICE: |
|
case NODE_NOTE: |
|
case NODE_TIP: |
|
case NODE_CAUTION: |
|
case NODE_WARNING: |
|
f->level--; |
|
break; |
|
case NODE_BLOCKQUOTE: |
|
case NODE_LITERALLAYOUT: |
|
case NODE_PROGRAMLISTING: |
|
case NODE_SCREEN: |
|
case NODE_SYNOPSIS: |
|
macro_line(f, "Ed"); |
|
break; |
default: |
default: |
break; |
break; |
} |
} |
|
f->flags &= ~FMT_ARG; |
} |
} |
|
|
/* |
void |
* Loop around the read buffer until we've drained it of all data. |
ptree_print_mdoc(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->root); |
|
pnode_print(&formatter, tree->root); |
XML_SetCharacterDataHandler(xp, xml_char); |
if (formatter.linestate != LINE_NEW) |
XML_SetElementHandler(xp, xml_elem_start, xml_elem_end); |
putchar('\n'); |
XML_SetUserData(xp, &p); |
|
|
|
while ((ssz = read(fd, b, bsz)) >= 0) { |
|
if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz))) |
|
fprintf(stderr, "%s: %s\n", fn, |
|
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. |
|
*/ |
|
pnode_print(&p, p.root); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return(0 != rc && ! p.stop); |
|
} |
|
|
|
/* Read error has occured. */ |
|
perror(fn); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return(0); |
|
} |
|
|
|
int |
|
main(int argc, char *argv[]) |
|
{ |
|
XML_Parser xp; |
|
const char *fname; |
|
char *buf; |
|
int fd, rc; |
|
|
|
fname = "-"; |
|
xp = NULL; |
|
buf = NULL; |
|
rc = 0; |
|
|
|
if (-1 != getopt(argc, argv, "")) |
|
return(EXIT_FAILURE); |
|
|
|
argc -= optind; |
|
argv += optind; |
|
|
|
if (argc > 1) |
|
return(EXIT_FAILURE); |
|
else if (argc > 0) |
|
fname = argv[0]; |
|
|
|
/* Read from stdin or a file. */ |
|
fd = 0 == strcmp(fname, "-") ? |
|
STDIN_FILENO : open(fname, O_RDONLY, 0); |
|
|
|
/* |
|
* Open file for reading. |
|
* Allocate a read buffer. |
|
* Create the parser context. |
|
* Dive directly into the parse. |
|
*/ |
|
if (-1 == fd) |
|
perror(fname); |
|
else if (NULL == (buf = malloc(4096))) |
|
perror(NULL); |
|
else if (NULL == (xp = XML_ParserCreate(NULL))) |
|
perror(NULL); |
|
else if ( ! readfile(xp, fd, buf, 4096, fname)) |
|
rc = 1; |
|
|
|
XML_ParserFree(xp); |
|
free(buf); |
|
if (STDIN_FILENO != fd) |
|
close(fd); |
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
|
} |
} |