version 1.8, 2014/03/28 12:11:18 |
version 1.143, 2019/04/28 19:59:01 |
|
|
/* $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 <unistd.h> |
|
|
|
/* |
#include "xmalloc.h" |
* All recognised node types. |
#include "node.h" |
*/ |
#include "macro.h" |
enum nodeid { |
#include "format.h" |
NODE_ROOT = 0, /* Must comes first. */ |
|
/* Alpha-ordered hereafter. */ |
|
NODE_ARG, |
|
NODE_CITEREFENTRY, |
|
NODE_CMDSYNOPSIS, |
|
NODE_CODE, |
|
NODE_COMMAND, |
|
NODE_FUNCDEF, |
|
NODE_FUNCPROTOTYPE, |
|
NODE_FUNCSYNOPSIS, |
|
NODE_FUNCSYNOPSISINFO, |
|
NODE_FUNCTION, |
|
NODE_MANVOLNUM, |
|
NODE_OPTION, |
|
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_STRUCTNAME, |
|
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; /* nil-terminated buffer for pre-print */ |
|
size_t bsz; /* current length of b */ |
|
size_t mbsz; /* max bsz allocation */ |
|
}; |
|
|
|
struct node { |
static void pnode_print(struct format *, struct pnode *); |
const char *name; /* docbook element name */ |
|
unsigned int flags; |
|
#define NODE_IGNTEXT 1 /* ignore all contained text */ |
|
}; |
|
|
|
TAILQ_HEAD(pnodeq, pnode); |
|
|
|
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 }, |
|
{ "arg", 0 }, |
|
{ "citerefentry", NODE_IGNTEXT }, |
|
{ "cmdsynopsis", NODE_IGNTEXT }, |
|
{ "code", 0 }, |
|
{ "command", 0 }, |
|
{ "funcdef", 0 }, |
|
{ "funcprototype", NODE_IGNTEXT }, |
|
{ "funcsynopsis", NODE_IGNTEXT }, |
|
{ "funcsynopsisinfo", 0 }, |
|
{ "function", 0 }, |
|
{ "manvolnum", 0 }, |
|
{ "option", 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 }, |
|
{ "structname", 0 }, |
|
{ "synopsis", 0 }, |
|
{ NULL, 0 }, |
|
{ "title", 0 }, |
|
}; |
|
|
|
/* |
|
* Look up whether "parent" is a valid parent for "node". |
|
* This is sucked directly from the DocBook specification: look at the |
|
* "children" and "parent" sections of each 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 && !accept_arg && |
case (NODE_ARG): |
(n->flags & NFLAG_SPC) == 0) { |
switch (parent) { |
for (;;) { |
case (NODE_ARG): |
if (*cp == '\0') |
case (NODE_CMDSYNOPSIS): |
return; |
return(1); |
if (strchr("!),.:;?]", *cp) == NULL) |
default: |
break; |
break; |
printf(" %c", *cp++); |
} |
} |
return(0); |
if (isspace((unsigned char)*cp)) { |
case (NODE_CITEREFENTRY): |
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_CMDSYNOPSIS): |
if (f->linestate == LINE_MACRO && !accept_arg && |
switch (parent) { |
(f->flags & (FMT_CHILD | FMT_IMPL)) == 0) |
case (NODE_PARA): |
macro_close(f); |
case (NODE_REFSECT1): |
|
case (NODE_REFSYNOPSISDIV): |
/* |
return(1); |
* Text preceding a macro without intervening whitespace |
default: |
* requires a .Pf macro. |
|
* Set the spacing flag to avoid a redundant .Ns macro. |
|
*/ |
|
|
|
if (f->linestate != LINE_MACRO && |
|
(nn = TAILQ_NEXT(n, child)) != NULL && |
|
(nn->flags & NFLAG_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->flags |= NFLAG_SPC; |
break; |
break; |
} |
|
return(0); |
|
case (NODE_CODE): |
|
switch (parent) { |
|
case (NODE_FUNCSYNOPSISINFO): |
|
case (NODE_PARA): |
|
case (NODE_PROGRAMLISTING): |
|
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_COMMAND): |
|
switch (parent) { |
switch (f->linestate) { |
case (NODE_CMDSYNOPSIS): |
case LINE_NEW: |
case (NODE_FUNCSYNOPSISINFO): |
break; |
case (NODE_PARA): |
case LINE_TEXT: |
case (NODE_PROGRAMLISTING): |
if (n->flags & NFLAG_SPC) { |
case (NODE_REFDESCRIPTOR): |
if (n->flags & NFLAG_LINE && |
case (NODE_REFENTRYTITLE): |
pnode_class(n->node) == CLASS_TEXT) |
case (NODE_REFNAME): |
macro_close(f); |
case (NODE_REFPURPOSE): |
else |
case (NODE_SYNOPSIS): |
putchar(' '); |
case (NODE_TITLE): |
|
return(1); |
|
default: |
|
break; |
|
} |
} |
return(0); |
|
case (NODE_FUNCDEF): |
|
return(NODE_FUNCPROTOTYPE == parent); |
|
case (NODE_FUNCPROTOTYPE): |
|
return(NODE_FUNCSYNOPSIS == parent); |
|
case (NODE_FUNCSYNOPSIS): |
|
switch (parent) { |
|
case (NODE_PARA): |
|
case (NODE_REFSECT1): |
|
case (NODE_REFSYNOPSISDIV): |
|
return(1); |
|
default: |
|
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_PROGRAMLISTING): |
|
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_MANVOLNUM): |
|
switch (parent) { |
|
case (NODE_CITEREFENTRY): |
|
case (NODE_REFMETA): |
|
return(1); |
|
default: |
|
break; |
|
} |
|
return(0); |
|
case (NODE_OPTION): |
|
switch (parent) { |
|
case (NODE_ARG): |
|
case (NODE_FUNCSYNOPSISINFO): |
|
case (NODE_PARA): |
|
case (NODE_PROGRAMLISTING): |
|
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_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_PROGRAMLISTING): |
|
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_STRUCTNAME): |
|
switch (parent) { |
|
case (NODE_CODE): |
|
case (NODE_FUNCSYNOPSISINFO): |
|
case (NODE_FUNCTION): |
|
case (NODE_OPTION): |
|
case (NODE_PARA): |
|
case (NODE_PARAMETER): |
|
case (NODE_PROGRAMLISTING): |
|
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_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 == 0) |
|
macro_close(f); |
|
else if (n->flags & NFLAG_SPC || |
|
(f->flags & FMT_ARG) == 0 || |
|
(nn = TAILQ_PREV(n, pnodeq, child)) == NULL || |
|
pnode_class(nn->node) != CLASS_TEXT) |
|
putchar(' '); |
|
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); |
} |
} |
|
|
/* |
|
* 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 |
static void |
xml_char(void *arg, const XML_Char *p, int sz) |
pnode_printimagedata(struct format *f, struct pnode *n) |
{ |
{ |
struct parse *ps = arg; |
const char *cp; |
struct pnode *dat; |
|
int i; |
|
|
|
/* Stopped or no tree yet. */ |
if ((cp = pnode_getattr_raw(n, ATTRKEY_FILEREF, NULL)) == NULL) |
if (ps->stop || NODE_ROOT == ps->node) |
cp = pnode_getattr_raw(n, ATTRKEY_ENTITYREF, NULL); |
return; |
if (cp != NULL) { |
|
print_text(f, "[image:", ARG_SPACE); |
|
print_text(f, cp, ARG_SPACE); |
|
print_text(f, "]", 0); |
|
} else |
|
print_text(f, "[image]", ARG_SPACE); |
|
} |
|
|
/* Not supposed to be collecting text. */ |
static void |
assert(NULL != ps->cur); |
pnode_printrefnamediv(struct format *f, struct pnode *n) |
if (NODE_IGNTEXT & nodes[ps->node].flags) |
{ |
return; |
struct pnode *nc, *nn; |
|
int comma; |
|
|
/* |
f->parastate = PARA_HAVE; |
* Are we in the midst of processing text? |
macro_line(f, "Sh NAME"); |
* If we're not processing text right now, then create a text |
f->parastate = PARA_HAVE; |
* node for doing so. |
comma = 0; |
* However, don't do so unless we have some non-whitespace to |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
* process! |
if (nc->node != NODE_REFNAME) |
*/ |
continue; |
if (NODE_TEXT != ps->node) { |
if (comma) |
for (i = 0; i < sz; i++) |
macro_addarg(f, ",", ARG_SPACE); |
if ( ! isspace((int)p[i])) |
macro_open(f, "Nm"); |
break; |
macro_addnode(f, nc, ARG_SPACE); |
if (i == sz) |
pnode_unlink(nc); |
return; |
comma = 1; |
dat = calloc(1, sizeof(struct pnode)); |
} |
if (NULL == dat) { |
macro_close(f); |
perror(NULL); |
} |
exit(EXIT_FAILURE); |
|
} |
|
|
|
dat->node = ps->node = NODE_TEXT; |
/* |
dat->parent = ps->cur; |
* If the SYNOPSIS macro has a superfluous title, kill it. |
TAILQ_INIT(&dat->childq); |
*/ |
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
static void |
ps->cur = dat; |
pnode_printrefsynopsisdiv(struct format *f, struct pnode *n) |
assert(NULL != ps->root); |
{ |
|
struct pnode *nc, *nn; |
|
|
} |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) |
|
if (nc->node == NODE_TITLE) |
|
pnode_unlink(nc); |
|
|
/* Append to current buffer. */ |
f->parastate = PARA_HAVE; |
assert(sz >= 0); |
macro_line(f, "Sh SYNOPSIS"); |
ps->cur->b = realloc(ps->cur->b, |
f->parastate = PARA_HAVE; |
ps->cur->bsz + (size_t)sz); |
|
if (NULL == ps->cur->b) { |
|
perror(NULL); |
|
exit(EXIT_FAILURE); |
|
} |
|
memcpy(ps->cur->b + ps->cur->bsz, p, sz); |
|
ps->cur->bsz += (size_t)sz; |
|
} |
} |
|
|
/* |
/* |
* Begin an element. |
* Start a hopefully-named `Sh' section. |
* 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 |
static void |
xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts) |
pnode_printsection(struct format *f, struct pnode *n) |
{ |
{ |
struct parse *ps = arg; |
struct pnode *nc, *ncc; |
enum nodeid node; |
int flags, level; |
struct pnode *dat; |
|
|
|
if (ps->stop) |
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); |
switch (n->node) { |
ps->cur = ps->cur->parent; |
case NODE_SECTION: |
assert(NULL != ps->cur); |
case NODE_APPENDIX: |
ps->node = ps->cur->node; |
if (level == 1) |
|
flags |= ARG_UPPER; |
|
break; |
|
case NODE_SIMPLESECT: |
|
if (level < 2) |
|
level = 2; |
|
break; |
|
case NODE_NOTE: |
|
if (level < 3) |
|
level = 3; |
|
break; |
|
default: |
|
abort(); |
} |
} |
|
|
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; |
|
|
/* FIXME: do more with these error messages... */ |
switch (level) { |
if (NODE__MAX == node && NODE_ROOT == ps->node) { |
case 1: |
fprintf(stderr, "%s: ignoring node\n", name); |
macro_close(f); |
return; |
f->parastate = PARA_HAVE; |
} else if (NODE__MAX == node) { |
macro_open(f, "Sh"); |
fprintf(stderr, "%s: unknown node\n", name); |
break; |
ps->stop = 1; |
case 2: |
return; |
macro_close(f); |
} else if (NODE_ROOT == ps->node && NULL != ps->root) { |
f->parastate = PARA_HAVE; |
fprintf(stderr, "%s: reentering?\n", name); |
macro_open(f, "Ss"); |
ps->stop = 1; |
break; |
return; |
default: |
} else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) { |
if (f->parastate == PARA_MID) |
fprintf(stderr, "%s: known node w/o context\n", name); |
f->parastate = PARA_WANT; |
return; |
macro_open(f, "Sy"); |
} else if ( ! isparent(node, ps->node)) { |
break; |
fprintf(stderr, "%s: bad parent\n", name); |
|
ps->stop = 1; |
|
return; |
|
} |
} |
|
macro_addnode(f, nc, flags); |
|
macro_close(f); |
|
|
if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { |
/* |
perror(NULL); |
* DocBook has no equivalent for -split mode, |
exit(EXIT_FAILURE); |
* so just switch the default in the AUTHORS section. |
} |
*/ |
|
|
dat->node = ps->node = node; |
if (nc != NULL) { |
dat->parent = ps->cur; |
if (level == 1 && |
TAILQ_INIT(&dat->childq); |
(ncc = TAILQ_FIRST(&nc->childq)) != NULL && |
|
ncc->node == NODE_TEXT && |
if (NULL != ps->cur) |
strcasecmp(ncc->b, "AUTHORS") == 0) |
TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); |
macro_line(f, "An -nosplit"); |
|
pnode_unlink(nc); |
ps->cur = dat; |
} |
if (NULL == ps->root) |
f->parastate = level > 2 ? PARA_WANT : PARA_HAVE; |
ps->root = dat; |
|
} |
} |
|
|
/* |
/* |
* Roll up the parse tree. |
* Start a reference, extracting the title and volume. |
* If we're at a text node, roll that one up first. |
|
* If we hit the root, then assign ourselves as the NODE_ROOT. |
|
*/ |
*/ |
static void |
static void |
xml_elem_end(void *arg, const XML_Char *name) |
pnode_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); |
} |
} |
|
|
/* |
/* |
* Recursively free a node (NULL is ok). |
* 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_free(struct pnode *pn) |
pnode_printmathfenced(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc; |
|
|
if (NULL == pn) |
printf("left %s ", pnode_getattr_raw(n, ATTRKEY_OPEN, "(")); |
return; |
|
|
|
while (NULL != (pp = TAILQ_FIRST(&pn->childq))) { |
nc = TAILQ_FIRST(&n->childq); |
TAILQ_REMOVE(&pn->childq, pp, child); |
pnode_print(f, nc); |
pnode_free(pp); |
|
} |
|
|
|
free(pn->b); |
while ((nc = TAILQ_NEXT(nc, child)) != NULL) { |
free(pn); |
putchar(','); |
|
pnode_print(f, nc); |
|
} |
|
printf("right %s ", pnode_getattr_raw(n, ATTRKEY_CLOSE, ")")); |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
/* |
* Unlink a node from its parent and pnode_free() it. |
* These math nodes require special handling because they have infix |
|
* syntax, instead of the usual prefix or prefix. |
|
* So we need to break up the first and second child node with a |
|
* particular eqn(7) word. |
*/ |
*/ |
static void |
static void |
pnode_unlink(struct pnode *pn) |
pnode_printmath(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *nc; |
|
|
if (NULL != pn->parent) |
nc = TAILQ_FIRST(&n->childq); |
TAILQ_REMOVE(&pn->parent->childq, pn, child); |
pnode_print(f, nc); |
pnode_free(pn); |
|
|
switch (n->node) { |
|
case NODE_MML_MSUP: |
|
fputs(" sup ", stdout); |
|
break; |
|
case NODE_MML_MFRAC: |
|
fputs(" over ", stdout); |
|
break; |
|
case NODE_MML_MSUB: |
|
fputs(" sub ", stdout); |
|
break; |
|
default: |
|
break; |
|
} |
|
|
|
nc = TAILQ_NEXT(nc, child); |
|
pnode_print(f, nc); |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
|
* Unlink all children of a node and pnode_free() them. |
|
*/ |
|
static void |
static void |
pnode_unlinksub(struct pnode *pn) |
pnode_printfuncprototype(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *fdef, *ftype, *nc, *nn; |
|
|
while ( ! TAILQ_EMPTY(&pn->childq)) |
/* |
pnode_unlink(TAILQ_FIRST(&pn->childq)); |
* Extract <funcdef> child and ignore <void> child. |
|
* Leave other children in place, to be treated as parameters. |
|
*/ |
|
|
|
fdef = NULL; |
|
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
|
switch (nc->node) { |
|
case NODE_FUNCDEF: |
|
if (fdef == NULL) { |
|
fdef = nc; |
|
TAILQ_REMOVE(&n->childq, nc, child); |
|
nc->parent = NULL; |
|
} |
|
break; |
|
case NODE_VOID: |
|
pnode_unlink(nc); |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
|
|
/* |
|
* If no children are left, the function is void; use .Fn. |
|
* Otherwise, use .Fo. |
|
*/ |
|
|
|
nc = TAILQ_FIRST(&n->childq); |
|
if (fdef != NULL) { |
|
ftype = TAILQ_FIRST(&fdef->childq); |
|
if (ftype != NULL && ftype->node == NODE_TEXT) { |
|
macro_argline(f, "Ft", ftype->b); |
|
pnode_unlink(ftype); |
|
} |
|
if (nc == NULL) { |
|
macro_open(f, "Fn"); |
|
macro_addnode(f, fdef, ARG_SPACE | ARG_SINGLE); |
|
macro_addarg(f, "void", ARG_SPACE); |
|
macro_close(f); |
|
} else |
|
macro_nodeline(f, "Fo", fdef, ARG_SINGLE); |
|
pnode_unlink(fdef); |
|
} else if (nc == NULL) |
|
macro_line(f, "Fn UNKNOWN void"); |
|
else |
|
macro_line(f, "Fo UNKNOWN"); |
|
|
|
if (nc == NULL) |
|
return; |
|
|
|
while (nc != NULL) { |
|
macro_nodeline(f, "Fa", nc, ARG_SINGLE); |
|
pnode_unlink(nc); |
|
nc = TAILQ_FIRST(&n->childq); |
|
} |
|
macro_line(f, "Fc"); |
} |
} |
|
|
/* |
/* |
* Reset the lookaside buffer. |
* 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 |
|
* 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 |
bufclear(struct parse *p) |
pnode_printarg(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *nc; |
|
struct pattr *a; |
|
int isop, isrep, was_impl; |
|
|
p->b[p->bsz = 0] = '\0'; |
isop = 1; |
|
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; |
|
} |
|
} |
|
TAILQ_FOREACH(nc, &n->childq, child) { |
|
if (nc->node == NODE_TEXT) |
|
macro_open(f, "Ar"); |
|
pnode_print(f, nc); |
|
} |
|
if (isrep && f->linestate == LINE_MACRO) |
|
macro_addarg(f, "...", ARG_SPACE); |
|
if (isop) { |
|
if (was_impl) |
|
macro_open(f, "Oc"); |
|
else |
|
f->flags &= ~FMT_IMPL; |
|
} |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
|
* Append NODE_TEXT contents to the current buffer, reallocating its |
|
* size if necessary. |
|
* The buffer is ALWAYS nil-terminated. |
|
*/ |
|
static void |
static void |
bufappend(struct parse *p, struct pnode *pn) |
pnode_printgroup(struct format *f, struct pnode *n) |
{ |
{ |
|
struct pnode *nc; |
|
struct pattr *a; |
|
int bar, isop, isrep, was_impl; |
|
|
assert(NODE_TEXT == pn->node); |
isop = 1; |
if (p->bsz + pn->bsz + 1 > p->mbsz) { |
isrep = was_impl = 0; |
p->mbsz = p->bsz + pn->bsz + 1; |
TAILQ_FOREACH(a, &n->attrq, child) { |
if (NULL == (p->b = realloc(p->b, p->mbsz))) { |
if (a->key == ATTRKEY_CHOICE && |
perror(NULL); |
(a->val == ATTRVAL_PLAIN || a->val == ATTRVAL_REQ)) |
exit(EXIT_FAILURE); |
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; |
} |
} |
|
} else if (isrep) { |
|
if (f->flags & FMT_IMPL) { |
|
was_impl = 1; |
|
macro_open(f, "Bro"); |
|
} else { |
|
macro_open(f, "Brq"); |
|
f->flags |= FMT_IMPL; |
|
} |
} |
} |
memcpy(p->b + p->bsz, pn->b, pn->bsz); |
bar = 0; |
p->bsz += pn->bsz; |
TAILQ_FOREACH(nc, &n->childq, child) { |
p->b[p->bsz] = '\0'; |
if (bar && f->linestate == LINE_MACRO) |
|
macro_addarg(f, "|", ARG_SPACE); |
|
pnode_print(f, nc); |
|
bar = 1; |
|
} |
|
if (isop) { |
|
if (was_impl) |
|
macro_open(f, "Oc"); |
|
else |
|
f->flags &= ~FMT_IMPL; |
|
} else if (isrep) { |
|
if (was_impl) |
|
macro_open(f, "Brc"); |
|
else |
|
f->flags &= ~FMT_IMPL; |
|
} |
|
if (isrep && f->linestate == LINE_MACRO) |
|
macro_addarg(f, "...", ARG_SPACE); |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
|
* 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 |
static void |
bufappend_r(struct parse *p, struct pnode *pn) |
pnode_printsystemitem(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
switch (pnode_getattr(n, ATTRKEY_CLASS)) { |
|
case ATTRVAL_IPADDRESS: |
if (NODE_TEXT == pn->node) |
break; |
bufappend(p, pn); |
case ATTRVAL_SYSTEMNAME: |
TAILQ_FOREACH(pp, &pn->childq, child) |
macro_open(f, "Pa"); |
bufappend_r(p, pp); |
break; |
|
case ATTRVAL_EVENT: |
|
default: |
|
macro_open(f, "Sy"); |
|
break; |
|
} |
} |
} |
|
|
/* |
|
* Recursively print text presumably on a macro line. |
|
* Convert all whitespace to regular spaces. |
|
*/ |
|
static void |
static void |
pnode_printmacrolinepart(struct parse *p, struct pnode *pn) |
pnode_printauthor(struct format *f, struct pnode *n) |
{ |
{ |
char *cp; |
struct pnode *nc, *nn; |
|
int have_contrib, have_name; |
|
|
bufclear(p); |
/* |
bufappend_r(p, pn); |
* Print <contrib> children up front, before the .An scope, |
|
* and figure out whether we a name of a person. |
|
*/ |
|
|
/* Convert all space to spaces. */ |
have_contrib = have_name = 0; |
for (cp = p->b; '\0' != *cp; cp++) |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
if (isspace((int)*cp)) |
switch (nc->node) { |
*cp = ' '; |
case NODE_CONTRIB: |
|
if (have_contrib) |
|
print_text(f, ",", 0); |
|
print_textnode(f, nc); |
|
pnode_unlink(nc); |
|
have_contrib = 1; |
|
break; |
|
case NODE_PERSONNAME: |
|
have_name = 1; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
if (TAILQ_FIRST(&n->childq) == NULL) |
|
return; |
|
|
for (cp = p->b; isspace((int)*cp); cp++) |
if (have_contrib) |
/* Spin past whitespace (XXX: necessary?) */ ; |
print_text(f, ":", 0); |
for ( ; '\0' != *cp; cp++) { |
|
/* Escape us if we look like a macro. */ |
/* |
if ((cp == p->b || ' ' == *(cp - 1)) && |
* If we have a name, print it in the .An scope and leave |
isupper((int)*cp) && |
* all other content for child handlers, to print after the |
'\0' != *(cp + 1) && |
* scope. Otherwise, print everything in the scope. |
islower((int)*(cp + 1)) && |
*/ |
('\0' == *(cp + 2) || |
|
' ' == *(cp + 2) || |
macro_open(f, "An"); |
(islower((int)*(cp + 2)) && |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
('\0' == *(cp + 3) || |
if (nc->node == NODE_PERSONNAME || have_name == 0) { |
' ' == *(cp + 3))))) |
macro_addnode(f, nc, ARG_SPACE); |
fputs("\\&", stdout); |
pnode_unlink(nc); |
putchar(*cp); |
} |
/* If we're a character escape, escape us. */ |
|
if ('\\' == *cp) |
|
putchar('e'); |
|
} |
} |
|
|
|
/* |
|
* 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); |
|
} |
} |
} |
|
|
/* |
|
* 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_printxref(struct format *f, struct pnode *n) |
{ |
{ |
|
const char *linkend; |
|
|
pnode_printmacrolinepart(p, pn); |
linkend = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL); |
putchar('\n'); |
if (linkend != NULL) { |
|
macro_open(f, "Sx"); |
|
macro_addarg(f, linkend, ARG_SPACE); |
|
} |
} |
} |
|
|
/* |
|
* Start the SYNOPSIS macro, unlinking its [superfluous] title. |
|
*/ |
|
static void |
static void |
pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn) |
pnode_printlink(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc; |
|
const char *uri, *text; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
uri = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL); |
if (NODE_TITLE == pp->node) { |
if (uri != NULL) { |
pnode_unlink(pp); |
if (TAILQ_FIRST(&n->childq) != NULL) { |
break; |
TAILQ_FOREACH(nc, &n->childq, child) |
|
pnode_print(f, nc); |
|
text = ""; |
|
} else if ((text = pnode_getattr_raw(n, |
|
ATTRKEY_ENDTERM, NULL)) != NULL) { |
|
if (f->linestate == LINE_MACRO && f->flags & FMT_ARG) |
|
macro_addarg(f, text, ARG_SPACE); |
|
else |
|
print_text(f, text, ARG_SPACE); |
} |
} |
|
if (text != NULL) { |
puts(".Sh SYNOPSIS"); |
if (f->flags & FMT_IMPL) |
|
macro_open(f, "Po"); |
|
else { |
|
macro_open(f, "Pq"); |
|
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); |
|
} |
} |
} |
|
|
/* |
|
* Start a hopefully-named `Sh' section. |
|
*/ |
|
static void |
static void |
pnode_printrefsect(struct parse *p, struct pnode *pn) |
pnode_printolink(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
const char *uri, *ptr, *local; |
|
|
TAILQ_FOREACH(pp, &pn->childq, child) |
uri = pnode_getattr_raw(n, ATTRKEY_TARGETDOC, NULL); |
if (NODE_TITLE == pp->node) |
ptr = pnode_getattr_raw(n, ATTRKEY_TARGETPTR, NULL); |
break; |
local = pnode_getattr_raw(n, ATTRKEY_LOCALINFO, NULL); |
|
if (uri == NULL) { |
|
uri = ptr; |
|
ptr = NULL; |
|
} |
|
if (uri == NULL) { |
|
uri = local; |
|
local = NULL; |
|
} |
|
if (uri == NULL) |
|
return; |
|
|
fputs(".Sh ", stdout); |
macro_open(f, "Lk"); |
|
macro_addarg(f, uri, ARG_SPACE | ARG_SINGLE); |
if (NULL != pp) { |
macro_addnode(f, n, ARG_SPACE | ARG_SINGLE); |
pnode_printmacroline(p, pp); |
if (ptr != NULL || local != NULL) { |
pnode_unlink(pp); |
macro_close(f); |
} else |
macro_open(f, "Pq"); |
puts("UNKNOWN"); |
if (ptr != NULL) |
|
macro_addarg(f, ptr, ARG_SPACE); |
|
if (local != NULL) |
|
macro_addarg(f, local, ARG_SPACE); |
|
} |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
|
* Start a reference, extracting the title and volume. |
|
*/ |
|
static void |
static void |
pnode_printciterefentry(struct parse *p, struct pnode *pn) |
pnode_printprologue(struct format *f, struct pnode *root) |
{ |
{ |
struct pnode *pp, *title, *manvol; |
struct pnode *name, *nc; |
|
|
title = manvol = NULL; |
nc = TAILQ_FIRST(&root->childq); |
TAILQ_FOREACH(pp, &pn->childq, child) |
assert(nc->node == NODE_DATE); |
if (NODE_MANVOLNUM == pp->node) |
macro_nodeline(f, "Dd", nc, 0); |
manvol = pp; |
pnode_unlink(nc); |
else if (NODE_REFENTRYTITLE == pp->node) |
|
title = pp; |
|
|
|
fputs(".Xr ", stdout); |
macro_open(f, "Dt"); |
|
name = TAILQ_FIRST(&root->childq); |
|
assert(name->node == NODE_REFENTRYTITLE); |
|
macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
|
TAILQ_REMOVE(&root->childq, name, child); |
|
name->parent = NULL; |
|
nc = TAILQ_FIRST(&root->childq); |
|
assert (nc->node == NODE_MANVOLNUM); |
|
macro_addnode(f, nc, ARG_SPACE | ARG_SINGLE); |
|
pnode_unlink(nc); |
|
|
if (NULL != title) { |
macro_line(f, "Os"); |
pnode_printmacrolinepart(p, title); |
|
putchar(' '); |
|
} else |
|
fputs("unknown ", stdout); |
|
|
|
if (NULL != manvol) |
nc = TAILQ_FIRST(&root->childq); |
pnode_printmacroline(p, manvol); |
if (nc != NULL && nc->node == NODE_TITLE) { |
else |
macro_line(f, "Sh NAME"); |
puts("1"); |
macro_nodeline(f, "Nm", name, ARG_SINGLE); |
|
macro_nodeline(f, "Nd", nc, 0); |
|
pnode_unlink(nc); |
|
} |
|
pnode_unlink(name); |
|
f->parastate = PARA_HAVE; |
} |
} |
|
|
|
/* |
|
* We can have multiple <term> elements within a <varlistentry>, which |
|
* we should comma-separate as list headers. |
|
*/ |
static void |
static void |
pnode_printrefmeta(struct parse *p, struct pnode *pn) |
pnode_printvarlistentry(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *title, *manvol; |
struct pnode *nc, *nn, *ncc; |
|
int comma; |
|
|
title = manvol = NULL; |
macro_open(f, "It"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
f->parastate = PARA_HAVE; |
if (NODE_MANVOLNUM == pp->node) |
f->flags |= FMT_IMPL; |
manvol = pp; |
comma = -1; |
else if (NODE_REFENTRYTITLE == pp->node) |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
title = pp; |
if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM) |
|
continue; |
puts(".Dd $Mdocdate" "$"); |
if (comma != -1) { |
fputs(".Dt ", stdout); |
switch (f->linestate) { |
|
case LINE_NEW: |
if (NULL != title) { |
break; |
/* FIXME: uppercase. */ |
case LINE_TEXT: |
pnode_printmacrolinepart(p, title); |
print_text(f, ",", 0); |
putchar(' '); |
break; |
} else |
case LINE_MACRO: |
fputs("UNKNOWN ", stdout); |
macro_addarg(f, ",", comma); |
|
break; |
if (NULL != manvol) |
} |
pnode_printmacroline(p, manvol); |
} |
else |
f->parastate = PARA_HAVE; |
puts("1"); |
comma = (ncc = TAILQ_FIRST(&nc->childq)) == NULL || |
|
pnode_class(ncc->node) == CLASS_TEXT ? 0 : ARG_SPACE; |
puts(".Os"); |
pnode_print(f, nc); |
|
pnode_unlink(nc); |
|
} |
|
macro_close(f); |
|
f->parastate = PARA_HAVE; |
|
while ((nc = TAILQ_FIRST(&n->childq)) != NULL) { |
|
pnode_print(f, nc); |
|
pnode_unlink(nc); |
|
} |
|
macro_close(f); |
|
f->parastate = PARA_HAVE; |
} |
} |
|
|
static void |
static void |
pnode_printfuncdef(struct parse *p, struct pnode *pn) |
pnode_printtitle(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *ftype, *func; |
struct pnode *nc, *nn; |
|
|
ftype = func = 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) |
if (f->parastate == PARA_MID) |
ftype = pp; |
f->parastate = PARA_WANT; |
else if (NODE_FUNCTION == pp->node) |
macro_nodeline(f, "Sy", nc, 0); |
func = pp; |
pnode_unlink(nc); |
|
} |
if (NULL != ftype) { |
|
fputs(".Ft ", stdout); |
|
pnode_printmacroline(p, ftype); |
|
} |
} |
|
|
if (NULL != func) { |
|
fputs(".Fo ", stdout); |
|
pnode_printmacroline(p, func); |
|
} else |
|
puts(".Fo UNKNOWN"); |
|
} |
} |
|
|
static void |
static void |
pnode_printparamdef(struct parse *p, struct pnode *pn) |
pnode_printrow(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp, *ptype, *param; |
struct pnode *nc; |
|
|
ptype = param = NULL; |
macro_line(f, "Bl -dash -compact"); |
TAILQ_FOREACH(pp, &pn->childq, child) |
TAILQ_FOREACH(nc, &n->childq, child) { |
if (NODE_TEXT == pp->node) |
macro_line(f, "It"); |
ptype = pp; |
pnode_print(f, nc); |
else if (NODE_PARAMETER == pp->node) |
|
param = pp; |
|
|
|
fputs(".Fa \"", stdout); |
|
if (NULL != ptype) { |
|
pnode_printmacrolinepart(p, ptype); |
|
putchar(' '); |
|
} |
} |
|
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"); |
|
f->parastate = PARA_HAVE; |
|
pnode_print(f, nc); |
|
f->parastate = PARA_HAVE; |
|
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) |
f->parastate = PARA_HAVE; |
if (NODE_FUNCDEF == fdef->node) |
macro_line(f, "Bl -tag -width Ds"); |
|
while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) { |
|
if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL) |
break; |
break; |
|
macro_open(f, "It"); |
if (NULL != fdef) |
f->flags |= FMT_IMPL; |
pnode_printfuncdef(p, fdef); |
f->parastate = PARA_HAVE; |
else |
pnode_print(f, ne); |
puts(".Fo UNKNOWN"); |
macro_close(f); |
|
pnode_unlink(ne); |
TAILQ_FOREACH(pp, &pn->childq, child) |
f->parastate = PARA_HAVE; |
if (NODE_PARAMDEF == pp->node) |
pnode_print(f, nr); |
pnode_printparamdef(p, pp); |
f->parastate = PARA_HAVE; |
|
pnode_unlink(nr); |
puts(".Fc"); |
} |
|
macro_line(f, "El"); |
|
f->parastate = PARA_WANT; |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* TODO: handle "optional" values. */ |
|
static void |
static void |
pnode_printarg(struct parse *p, struct pnode *pn, int nested) |
pnode_printtgroup(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc; |
int sv = nested; |
|
|
|
if ( ! nested) |
switch (atoi(pnode_getattr_raw(n, ATTRKEY_COLS, "0"))) { |
fputs(".", stdout); |
case 1: |
nested = 1; |
pnode_printtgroup1(f, n); |
TAILQ_FOREACH(pp, &pn->childq, child) |
return; |
if (NODE_OPTION == pp->node) { |
case 2: |
fputs("Fl ", stdout); |
pnode_printtgroup2(f, n); |
pnode_printmacrolinepart(p, pp); |
return; |
} else if (NODE_TEXT == pp->node) { |
default: |
fputs("Ar ", stdout); |
break; |
pnode_printmacrolinepart(p, pp); |
} |
} else if (NODE_ARG == pp->node) |
|
pnode_printarg(p, pp, nested); |
|
|
|
if ( ! sv) |
f->parastate = PARA_HAVE; |
puts(""); |
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"); |
|
f->parastate = PARA_WANT; |
|
pnode_unlinksub(n); |
} |
} |
|
|
/* |
static void |
* Recursively search and return the first instance of "node". |
pnode_printlist(struct format *f, struct pnode *n) |
*/ |
|
static struct pnode * |
|
pnode_findfirst(struct pnode *pn, enum nodeid node) |
|
{ |
{ |
struct pnode *pp, *res; |
struct pnode *nc; |
|
|
res = NULL; |
pnode_printtitle(f, n); |
TAILQ_FOREACH(pp, &pn->childq, child) { |
f->parastate = PARA_HAVE; |
res = pp->node == node ? pp : |
macro_argline(f, "Bl", |
pnode_findfirst(pp, node); |
n->node == NODE_ORDEREDLIST ? "-enum" : "-bullet"); |
if (NULL != res) |
TAILQ_FOREACH(nc, &n->childq, child) { |
break; |
macro_line(f, "It"); |
|
f->parastate = PARA_HAVE; |
|
pnode_print(f, nc); |
|
f->parastate = PARA_HAVE; |
} |
} |
|
macro_line(f, "El"); |
return(res); |
f->parastate = PARA_WANT; |
|
pnode_unlinksub(n); |
} |
} |
|
|
static void |
static void |
pnode_printprologue(struct parse *p, struct pnode *pn) |
pnode_printvariablelist(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *pp; |
struct pnode *nc; |
|
|
if (NULL != (pp = pnode_findfirst(p->root, NODE_REFMETA))) { |
pnode_printtitle(f, n); |
pnode_printrefmeta(p, pp); |
f->parastate = PARA_HAVE; |
pnode_unlink(pp); |
macro_line(f, "Bl -tag -width Ds"); |
} else { |
TAILQ_FOREACH(nc, &n->childq, child) { |
puts(".\\\" Supplying bogus prologue..."); |
if (nc->node == NODE_VARLISTENTRY) |
puts(".Dd $Mdocdate" "$"); |
pnode_printvarlistentry(f, nc); |
puts(".Dt UNKNOWN 1"); |
else |
puts(".Os"); |
macro_nodeline(f, "It", nc, 0); |
} |
} |
|
macro_line(f, "El"); |
|
f->parastate = PARA_WANT; |
|
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->flags & NFLAG_SPC) |
|
f->flags &= ~FMT_NOSPC; |
|
else |
|
f->flags |= FMT_NOSPC; |
|
|
switch (pn->node) { |
switch (n->node) { |
case (NODE_ARG): |
case NODE_ARG: |
pnode_printarg(p, pn, 0); |
pnode_printarg(f, n); |
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_CITEREFENTRY): |
case NODE_AUTHOR: |
pnode_printciterefentry(p, pn); |
pnode_printauthor(f, n); |
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_CODE): |
case NODE_AUTHORGROUP: |
fputs(".Li ", stdout); |
macro_line(f, "An -split"); |
pnode_printmacroline(p, pn); |
|
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_COMMAND): |
case NODE_BLOCKQUOTE: |
fputs(".Nm ", stdout); |
f->parastate = PARA_HAVE; |
pnode_printmacroline(p, pn); |
macro_line(f, "Bd -ragged -offset indent"); |
pnode_unlinksub(pn); |
f->parastate = PARA_HAVE; |
break; |
break; |
case (NODE_FUNCTION): |
case NODE_CITEREFENTRY: |
fputs(".Fn ", stdout); |
pnode_printciterefentry(f, n); |
pnode_printmacroline(p, pn); |
|
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_FUNCPROTOTYPE): |
case NODE_CITETITLE: |
pnode_printfuncprototype(p, pn); |
macro_open(f, "%T"); |
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_FUNCSYNOPSISINFO): |
case NODE_COMMAND: |
fputs(".Fd ", stdout); |
macro_open(f, "Nm"); |
pnode_printmacroline(p, pn); |
|
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_PARA): |
case NODE_CONSTANT: |
/* FIXME: not always. */ |
macro_open(f, "Dv"); |
puts(".Pp"); |
|
break; |
break; |
case (NODE_PARAMETER): |
case NODE_COPYRIGHT: |
fputs(".Fa \"", stdout); |
print_text(f, "Copyright", ARG_SPACE); |
pnode_printmacrolinepart(p, pn); |
fputs(" \\(co", stdout); |
puts("\""); |
|
pnode_unlinksub(pn); |
|
break; |
break; |
case (NODE_PROGRAMLISTING): |
case NODE_EDITOR: |
puts(".Bd -literal"); |
print_text(f, "editor:", ARG_SPACE); |
|
pnode_printauthor(f, n); |
break; |
break; |
case (NODE_REFMETA): |
case NODE_EMAIL: |
abort(); |
if (was_impl) |
|
macro_open(f, "Ao Mt"); |
|
else { |
|
macro_open(f, "Aq Mt"); |
|
f->flags |= FMT_IMPL; |
|
} |
break; |
break; |
case (NODE_REFNAME): |
case NODE_EMPHASIS: |
fputs(".Nm ", stdout); |
case NODE_FIRSTTERM: |
pnode_printmacroline(p, pn); |
case NODE_GLOSSTERM: |
pnode_unlinksub(pn); |
if ((nc = TAILQ_FIRST(&n->childq)) != NULL && |
return; |
pnode_class(nc->node) < CLASS_LINE) |
case (NODE_REFNAMEDIV): |
macro_open(f, "Em"); |
puts(".Sh NAME"); |
if (n->node == NODE_GLOSSTERM) |
|
f->parastate = PARA_HAVE; |
break; |
break; |
case (NODE_REFPURPOSE): |
case NODE_ENVAR: |
fputs(".Nd ", stdout); |
macro_open(f, "Ev"); |
pnode_printmacroline(p, pn); |
|
pnode_unlinksub(pn); |
|
return; |
|
case (NODE_REFSYNOPSISDIV): |
|
pnode_printrefsynopsisdiv(p, pn); |
|
break; |
break; |
case (NODE_REFSECT1): |
case NODE_ERRORNAME: |
pnode_printrefsect(p, pn); |
macro_open(f, "Er"); |
break; |
break; |
case (NODE_STRUCTNAME): |
case NODE_FILENAME: |
fputs(".Vt ", stdout); |
macro_open(f, "Pa"); |
pnode_printmacroline(p, pn); |
break; |
pnode_unlinksub(pn); |
case NODE_FOOTNOTE: |
return; |
macro_line(f, "Bo"); |
case (NODE_TEXT): |
f->parastate = PARA_HAVE; |
bufclear(p); |
break; |
bufappend(p, pn); |
case NODE_FUNCTION: |
/* |
macro_open(f, "Fn"); |
* Output all characters, squeezing out whitespace |
break; |
* between newlines. |
case NODE_FUNCPROTOTYPE: |
* XXX: all whitespace, including tabs (?). |
pnode_printfuncprototype(f, n); |
* Remember to escape control characters and escapes. |
break; |
*/ |
case NODE_FUNCSYNOPSISINFO: |
for (last = '\n', cp = p->b; '\0' != *cp; ) { |
macro_open(f, "Fd"); |
if ('\n' == last) { |
break; |
/* Consume all whitespace. */ |
case NODE_IMAGEDATA: |
if (isspace((int)*cp)) { |
pnode_printimagedata(f, n); |
while (isspace((int)*cp)) |
break; |
cp++; |
case NODE_INFORMALEQUATION: |
continue; |
f->parastate = PARA_HAVE; |
} else if ('\'' == *cp || '.' == *cp) |
macro_line(f, "Bd -ragged -offset indent"); |
fputs("\\&", stdout); |
f->parastate = PARA_HAVE; |
} |
/* FALLTHROUGH */ |
putchar(last = *cp++); |
case NODE_INLINEEQUATION: |
/* If we're a character escape, escape us. */ |
macro_line(f, "EQ"); |
if ('\\' == last) |
break; |
putchar('e'); |
case NODE_ITEMIZEDLIST: |
|
pnode_printlist(f, n); |
|
break; |
|
case NODE_GROUP: |
|
pnode_printgroup(f, n); |
|
break; |
|
case NODE_KEYSYM: |
|
case NODE_PRODUCTNAME: |
|
macro_open(f, "Sy"); |
|
break; |
|
case NODE_LINK: |
|
pnode_printlink(f, n); |
|
break; |
|
case NODE_LITERAL: |
|
if (n->parent != NULL && n->parent->node == NODE_QUOTE) |
|
macro_open(f, "Li"); |
|
else if (was_impl) |
|
macro_open(f, "So Li"); |
|
else { |
|
macro_open(f, "Ql"); |
|
f->flags |= FMT_IMPL; |
} |
} |
if ('\n' != last) |
|
putchar('\n'); |
|
break; |
break; |
|
case NODE_LITERALLAYOUT: |
|
macro_close(f); |
|
f->parastate = PARA_HAVE; |
|
macro_argline(f, "Bd", pnode_getattr(n, ATTRKEY_CLASS) == |
|
ATTRVAL_MONOSPACED ? "-literal" : "-unfilled"); |
|
f->parastate = PARA_HAVE; |
|
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_OLINK: |
|
pnode_printolink(f, n); |
|
break; |
|
case NODE_OPTION: |
|
if ((nc = TAILQ_FIRST(&n->childq)) != NULL && |
|
pnode_class(nc->node) < CLASS_LINE) |
|
macro_open(f, "Fl"); |
|
break; |
|
case NODE_ORDEREDLIST: |
|
pnode_printlist(f, n); |
|
break; |
|
case NODE_PARA: |
|
if (f->parastate == PARA_MID) |
|
f->parastate = PARA_WANT; |
|
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 ((nc = TAILQ_FIRST(&n->childq)) != NULL && |
|
nc->node == NODE_FILENAME && |
|
TAILQ_NEXT(nc, child) == NULL) { |
|
if (n->flags & NFLAG_SPC) |
|
nc->flags |= NFLAG_SPC; |
|
} else 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: |
|
f->parastate = PARA_HAVE; |
|
macro_line(f, "Bd -literal"); |
|
f->parastate = PARA_HAVE; |
|
break; |
|
case NODE_SYSTEMITEM: |
|
pnode_printsystemitem(f, 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_SECTION: |
|
case NODE_SIMPLESECT: |
|
case NODE_APPENDIX: |
|
case NODE_NOTE: |
|
pnode_printsection(f, n); |
|
break; |
|
case NODE_REPLACEABLE: |
|
macro_open(f, "Ar"); |
|
break; |
|
case NODE_SBR: |
|
if (f->parastate == PARA_MID) |
|
macro_line(f, "br"); |
|
break; |
|
case NODE_SUBSCRIPT: |
|
if (f->linestate == LINE_MACRO) |
|
macro_addarg(f, "_", 0); |
|
else |
|
print_text(f, "_", 0); |
|
if ((nc = TAILQ_FIRST(&n->childq)) != NULL) |
|
nc->flags &= ~(NFLAG_LINE | NFLAG_SPC); |
|
break; |
|
case NODE_SUPERSCRIPT: |
|
fputs("\\(ha", stdout); |
|
if ((nc = TAILQ_FIRST(&n->childq)) != NULL) |
|
nc->flags &= ~(NFLAG_LINE | NFLAG_SPC); |
|
break; |
|
case NODE_TEXT: |
|
case NODE_ESCAPE: |
|
pnode_printtext(f, n); |
|
break; |
|
case NODE_TGROUP: |
|
pnode_printtgroup(f, n); |
|
break; |
|
case NODE_TITLE: |
|
case NODE_SUBTITLE: |
|
if (f->parastate == PARA_MID) |
|
f->parastate = PARA_WANT; |
|
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; |
|
case NODE_VOID: |
|
print_text(f, "void", ARG_SPACE); |
|
break; |
|
case NODE_XREF: |
|
pnode_printxref(f, n); |
|
break; |
|
case NODE_CAUTION: |
|
case NODE_LEGALNOTICE: |
|
case NODE_PREFACE: |
|
case NODE_TIP: |
|
case NODE_WARNING: |
|
abort(); |
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_FOOTNOTE: |
|
f->parastate = PARA_HAVE; |
|
macro_line(f, "Bc"); |
|
break; |
|
case NODE_GLOSSTERM: |
|
f->parastate = PARA_HAVE; |
|
break; |
|
case NODE_INFORMALEQUATION: |
|
macro_line(f, "EN"); |
|
macro_line(f, "Ed"); |
|
break; |
|
case NODE_INLINEEQUATION: |
|
macro_line(f, "EN"); |
|
break; |
|
case NODE_LITERAL: |
|
if (n->parent != NULL && n->parent->node == NODE_QUOTE) |
|
/* nothing */; |
|
else 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_PARA: |
|
if (f->parastate == PARA_MID) |
|
f->parastate = PARA_WANT; |
|
break; |
|
case NODE_QUOTE: |
|
if ((nc = TAILQ_FIRST(&n->childq)) != NULL && |
|
nc->node == NODE_FILENAME && |
|
TAILQ_NEXT(nc, child) == NULL) |
|
/* nothing */; |
|
else if (was_impl) { |
|
f->flags &= ~FMT_NOSPC; |
|
macro_open(f, "Dc"); |
|
} else |
|
f->flags &= ~FMT_IMPL; |
|
break; |
|
case NODE_SECTION: |
|
case NODE_SIMPLESECT: |
|
case NODE_APPENDIX: |
|
case NODE_NOTE: |
|
if (n->parent != NULL) |
|
f->level--; |
|
break; |
|
case NODE_BLOCKQUOTE: |
|
case NODE_LITERALLAYOUT: |
|
case NODE_PROGRAMLISTING: |
|
case NODE_SCREEN: |
|
case NODE_SYNOPSIS: |
|
f->parastate = PARA_HAVE; |
|
macro_line(f, "Ed"); |
|
f->parastate = PARA_WANT; |
|
break; |
|
case NODE_TITLE: |
|
case NODE_SUBTITLE: |
|
f->parastate = PARA_WANT; |
|
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); |
formatter.parastate = PARA_HAVE; |
|
pnode_printprologue(&formatter, tree->root); |
XML_SetCharacterDataHandler(xp, xml_char); |
pnode_print(&formatter, tree->root); |
XML_SetElementHandler(xp, xml_elem_start, xml_elem_end); |
if (formatter.linestate != LINE_NEW) |
XML_SetUserData(xp, &p); |
putchar('\n'); |
|
|
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_printprologue(&p, p.root); |
|
pnode_print(&p, p.root); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return(0 != rc && ! p.stop); |
|
} |
|
|
|
/* Read error has occured. */ |
|
perror(fn); |
|
pnode_free(p.root); |
|
free(p.b); |
|
return(0); |
|
} |
|
|
|
int |
|
main(int argc, char *argv[]) |
|
{ |
|
XML_Parser xp; |
|
const char *fname; |
|
char *buf; |
|
int fd, rc; |
|
|
|
fname = "-"; |
|
xp = NULL; |
|
buf = NULL; |
|
rc = 0; |
|
|
|
if (-1 != getopt(argc, argv, "")) |
|
return(EXIT_FAILURE); |
|
|
|
argc -= optind; |
|
argv += optind; |
|
|
|
if (argc > 1) |
|
return(EXIT_FAILURE); |
|
else if (argc > 0) |
|
fname = argv[0]; |
|
|
|
/* Read from stdin or a file. */ |
|
fd = 0 == strcmp(fname, "-") ? |
|
STDIN_FILENO : open(fname, O_RDONLY, 0); |
|
|
|
/* |
|
* Open file for reading. |
|
* Allocate a read buffer. |
|
* Create the parser context. |
|
* Dive directly into the parse. |
|
*/ |
|
if (-1 == fd) |
|
perror(fname); |
|
else if (NULL == (buf = malloc(4096))) |
|
perror(NULL); |
|
else if (NULL == (xp = XML_ParserCreate(NULL))) |
|
perror(NULL); |
|
else if ( ! readfile(xp, fd, buf, 4096, fname)) |
|
rc = 1; |
|
|
|
XML_ParserFree(xp); |
|
free(buf); |
|
if (STDIN_FILENO != fd) |
|
close(fd); |
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
|
} |
} |