[BACK]Return to docbook2mdoc.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / docbook2mdoc

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.10 and 1.134

version 1.10, 2014/03/29 10:56:21 version 1.134, 2019/04/23 22:25:28
Line 1 
Line 1 
 /*      $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
Line 14 
Line 15 
  * 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 "node.h"
  * All recognised node types.  #include "macro.h"
  */  #include "format.h"
 enum    nodeid {  
         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 */  
         int              newln; /* output: are we on a fresh line */  
 };  
   
 struct  node {  static void      pnode_print(struct format *, struct pnode *);
         const char      *name; /* docbook element name */  static void      pnode_printrefentry(struct format *, struct pnode *);
         unsigned int     flags;  
 #define NODE_IGNTEXT     1 /* ignore all contained text */  
 };  
   
 TAILQ_HEAD(pnodeq, pnode);  
   
 struct  pnode {  
         enum nodeid      node; /* node type */  
         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 },  
 };  
   
 static void  static void
 pnode_print(struct parse *p, struct pnode *pn);  pnode_printtext(struct format *f, struct pnode *n)
   
 /*  
  * 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 && !n->spc && !accept_arg) {
         case (NODE_ARG):                  for (;;) {
                 switch (parent) {                          if (*cp == '\0')
                 case (NODE_ARG):                                  return;
                 case (NODE_CMDSYNOPSIS):                          if (strchr("!),.:;?]", *cp) == NULL)
                         return(1);                                  break;
                 default:                          printf(" %c", *cp++);
                         break;  
                 }                  }
                 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->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_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->spc) {
                 case (NODE_REFDESCRIPTOR):                          if (n->node == NODE_TEXT)
                 case (NODE_REFENTRYTITLE):                                  macro_close(f);
                 case (NODE_REFNAME):                          else
                 case (NODE_REFPURPOSE):                                  putchar(' ');
                 case (NODE_SYNOPSIS):  
                 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)
                           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);
 }  }
   
 /*  
  * 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: strip out all leading whitespace to be sure.                  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;
                 p += i;  
                 sz -= i;  
                 dat = calloc(1, sizeof(struct pnode));  
                 if (NULL == dat) {  
                         perror(NULL);  
                         exit(EXIT_FAILURE);  
                 }  
   
                 dat->node = ps->node = NODE_TEXT;  
                 dat->parent = ps->cur;  
                 TAILQ_INIT(&dat->childq);  
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);  
                 ps->cur = dat;  
                 assert(NULL != ps->root);  
         }          }
           macro_close(f);
         /* Append to current buffer. */  
         assert(sz >= 0);  
         ps->cur->b = realloc(ps->cur->b,  
                 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;  
 }  }
   
   /*
    * If the SYNOPSIS macro has a superfluous title, kill it.
    */
 static void  static void
 pnode_trim(struct pnode *pn)  pnode_printrefsynopsisdiv(struct format *f, struct pnode *n)
 {  {
           struct pnode    *nc, *nn;
   
         assert(NODE_TEXT == pn->node);          TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn)
         for ( ; pn->bsz > 0; pn->bsz--)                  if (nc->node == NODE_TITLE)
                 if ( ! isspace((int)pn->b[pn->bsz - 1]))                          pnode_unlink(nc);
                         break;  
           f->parastate = PARA_HAVE;
           macro_line(f, "Sh SYNOPSIS");
           f->parastate = PARA_HAVE;
 }  }
   
 /*  /*
  * 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;          const char      *title;
         struct pnode    *dat;          int              flags, level;
   
         if (ps->stop)          if (n->parent == NULL) {
                   pnode_printrefentry(f, n);
                 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) {
                 pnode_trim(ps->cur);          case NODE_PREFACE:
                 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:
           case NODE_LEGALNOTICE:
                   if (level < 2)
                           level = 2;
                   break;
           default:
                   if (level < 3)
                           level = 3;
                   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;
   
         /* FIXME: do more with these error messages... */          if (nc == NULL) {
         if (NODE__MAX == node && NODE_ROOT == ps->node) {                  switch (n->node) {
                 fprintf(stderr, "%s: ignoring node\n", name);                  case NODE_PREFACE:
                 return;                          title = "Preface";
         } else if (NODE__MAX == node) {                          break;
                 fprintf(stderr, "%s: unknown node\n", name);                  case NODE_APPENDIX:
                 ps->stop = 1;                          title = "Appendix";
                 return;                          break;
         } else if (NODE_ROOT == ps->node && NULL != ps->root) {                  case NODE_LEGALNOTICE:
                 fprintf(stderr, "%s: reentering?\n", name);                          title = "Legal Notice";
                 ps->stop = 1;                          break;
                 return;                  case NODE_CAUTION:
         } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {                          title = "Caution";
                 fprintf(stderr, "%s: known node w/o context\n", name);                          break;
                 return;                  case NODE_NOTE:
         } else if ( ! isparent(node, ps->node)) {                          title = "Note";
                 fprintf(stderr, "%s: bad parent\n", name);                          break;
                 ps->stop = 1;                  case NODE_TIP:
                 return;                          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);
                   f->parastate = PARA_HAVE;
                   macro_open(f, "Sh");
                   break;
           case 2:
                   macro_close(f);
                   f->parastate = PARA_HAVE;
                   macro_open(f, "Ss");
                   break;
           default:
                   if (f->parastate == PARA_MID)
                           f->parastate = PARA_WANT;
                   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)                  if (level == 1 &&
                 ps->root = dat;                      (ncc = TAILQ_FIRST(&nc->childq)) != NULL &&
                       ncc->node == NODE_TEXT &&
                       strcasecmp(ncc->b, "AUTHORS") == 0)
                           macro_line(f, "An -nosplit");
                   pnode_unlink(nc);
           }
           f->parastate = level > 2 ? PARA_WANT : PARA_HAVE;
 }  }
   
 /*  /*
  * 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;
                 pnode_trim(ps->cur);  
                 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');  
         }          }
 }  
   
 /*          /*
  * Just pnode_printmacrolinepart() but with a newline.           * If there is an email address,
  * If no text, just the newline.           * print it on the same macro line.
  */           */
 static void  
 pnode_printmacroline(struct parse *p, struct pnode *pn)  
 {  
   
         pnode_printmacrolinepart(p, pn);          if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) {
         putchar('\n');                  f->flags |= FMT_CHILD;
 }                  macro_open(f, "Aq Mt");
                   macro_addnode(f, nc, ARG_SPACE);
                   pnode_unlink(nc);
           }
   
 static void          /*
 pnode_printmopen(struct parse *p)           * If there are still unprinted children, end the scope
 {           * with a comma.  Otherwise, leave the scope open in case
         if (p->newln) {           * a text node follows that starts with closing punctuation.
                 putchar('.');           */
                 p->newln = 0;  
         } else          if (TAILQ_FIRST(&n->childq) != NULL) {
                 putchar(' ');                  macro_addarg(f, ",", ARG_SPACE);
                   macro_close(f);
           }
 }  }
   
 static void  static void
 pnode_printmclose(struct parse *p, int sv)  pnode_printxref(struct format *f, struct pnode *n)
 {  {
           const char      *linkend;
   
         if (sv && ! p->newln) {          linkend = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL);
                 putchar('\n');          if (linkend != NULL) {
                 p->newln = 1;                  macro_open(f, "Sx");
                   macro_addarg(f, linkend, ARG_SPACE);
         }          }
 }  }
   
 /*  
  * If the SYNOPSIS macro has a superfluous title, kill it.  
  */  
 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) {
                         return;                          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) {
                           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    *date, *refmeta, *name, *vol, *descr, *nc, *nn;
           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;  
   
         fputs(".Xr ", stdout);          if ((date = pnode_takefirst(root, NODE_PUBDATE)) == NULL)
                   date = pnode_takefirst(root, NODE_DATE);
   
         if (NULL != title) {          name = vol = NULL;
                 pnode_printmacrolinepart(p, title);          if ((refmeta = pnode_findfirst(root, NODE_REFMETA)) != NULL) {
                 putchar(' ');                  TAILQ_FOREACH_SAFE(nc, &refmeta->childq, child, nn) {
         } else                          switch (nc->node) {
                 fputs("unknown ", stdout);                          case NODE_REFENTRYTITLE:
                                   name = nc;
                                   break;
                           case NODE_MANVOLNUM:
                                   vol = nc;
                                   break;
                           default:
                                   continue;
                           }
                           TAILQ_REMOVE(&refmeta->childq, nc, child);
                   }
           }
   
         if (NULL != manvol)          if (pnode_findfirst(root, NODE_REFNAMEDIV) == NULL &&
                 pnode_printmacroline(p, manvol);              ((nc = pnode_findfirst(root, NODE_BOOKINFO)) != NULL ||
                (nc = pnode_findfirst(root, NODE_REFENTRYINFO)) != NULL))
                   descr = pnode_takefirst(nc, NODE_TITLE);
         else          else
                 puts("1");                  descr = NULL;
 }  
   
 static void          /* Print prologue. */
 pnode_printrefmeta(struct parse *p, struct pnode *pn)  
 {  
         struct pnode    *pp, *title, *manvol;  
   
         title = manvol = NULL;          if (date == NULL)
         TAILQ_FOREACH(pp, &pn->childq, child)                  macro_line(f, "Dd $Mdocdate" "$");
                 if (NODE_MANVOLNUM == pp->node)          else
                         manvol = pp;                  macro_nodeline(f, "Dd", date, 0);
                 else if (NODE_REFENTRYTITLE == pp->node)  
                         title = pp;  
   
         puts(".Dd $Mdocdate" "$");          macro_open(f, "Dt");
         fputs(".Dt ", stdout);          if (name == NULL) {
                   sname = pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN");
         if (NULL != title) {                  macro_addarg(f, sname, ARG_SPACE | ARG_SINGLE | ARG_UPPER);
                 /* FIXME: uppercase. */  
                 pnode_printmacrolinepart(p, title);  
                 putchar(' ');  
         } else          } else
                 fputs("UNKNOWN ", stdout);                  macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER);
           if (vol == NULL)
         if (NULL != manvol)                  macro_addarg(f, "1", ARG_SPACE);
                 pnode_printmacroline(p, manvol);  
         else          else
                 puts("1");                  macro_addnode(f, vol, ARG_SPACE | ARG_SINGLE);
   
         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(name);
           pnode_unlink(vol);
           pnode_unlink(descr);
           f->parastate = PARA_HAVE;
 }  }
   
 static void  static void
 pnode_printfuncdef(struct parse *p, struct pnode *pn)  pnode_printrefentry(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp, *ftype, *func;          struct pnode    *info, *meta, *nc, *title;
           struct pnode    *match, *later;
   
         ftype = func = NULL;          /* Collect nodes that remained behind when writing the prologue. */
         TAILQ_FOREACH(pp, &pn->childq, child)  
                 if (NODE_TEXT == pp->node)  
                         ftype = pp;  
                 else if (NODE_FUNCTION == pp->node)  
                         func = pp;  
   
         if (NULL != ftype) {          meta = NULL;
                 fputs(".Ft ", stdout);          info = pnode_takefirst(n, NODE_BOOKINFO);
                 pnode_printmacroline(p, ftype);          if (info != NULL && TAILQ_FIRST(&info->childq) == NULL) {
                   pnode_unlink(info);
                   info = NULL;
         }          }
           if (info == NULL) {
                   info = pnode_takefirst(n, NODE_REFENTRYINFO);
                   if (info != NULL && TAILQ_FIRST(&info->childq) == NULL) {
                           pnode_unlink(info);
                           info = NULL;
                   }
                   if (info == NULL)
                           info = pnode_takefirst(n, NODE_INFO);
                   meta = pnode_takefirst(n, NODE_REFMETA);
                   if (meta != NULL && TAILQ_FIRST(&meta->childq) == NULL) {
                           pnode_unlink(meta);
                           meta = NULL;
                   }
           }
           if (info == NULL && meta == NULL)
                   return;
   
         if (NULL != func) {          /*
                 fputs(".Fo ", stdout);           * Find the best place to put this information.
                 pnode_printmacroline(p, func);           * Use the last existing AUTHORS node, if any.
         } else           * Otherwise, put it behind all standard sections that
                 puts(".Fo UNKNOWN");           * conventionally precede AUTHORS, and also behind any
 }           * non-standard sections that follow the last of these,
            * but before the next standard section.
            */
   
 static void          match = later = NULL;
 pnode_printparamdef(struct parse *p, struct pnode *pn)          TAILQ_FOREACH(nc, &n->childq, child) {
 {                  switch (nc->node) {
         struct pnode    *pp, *ptype, *param;                  case NODE_REFENTRY:
                   case NODE_REFNAMEDIV:
                   case NODE_REFSYNOPSISDIV:
                   case NODE_PREFACE:
                           later = NULL;
                           continue;
                   case NODE_APPENDIX:
                   case NODE_INDEX:
                           if (later == NULL)
                                   later = nc;
                           continue;
                   default:
                           break;
                   }
                   if ((title = pnode_findfirst(nc, NODE_TITLE)) == NULL ||
                       (title = TAILQ_FIRST(&title->childq)) == NULL ||
                       title->node != NODE_TEXT)
                           continue;
                   if (strcasecmp(title->b, "AUTHORS") == 0 ||
                       strcasecmp(title->b, "AUTHOR") == 0)
                           match = nc;
                   else if (strcasecmp(title->b, "NAME") == 0 ||
                       strcasecmp(title->b, "SYNOPSIS") == 0 ||
                       strcasecmp(title->b, "DESCRIPTION") == 0 ||
                       strcasecmp(title->b, "RETURN VALUES") == 0 ||
                       strcasecmp(title->b, "ENVIRONMENT") == 0 ||
                       strcasecmp(title->b, "FILES") == 0 ||
                       strcasecmp(title->b, "EXIT STATUS") == 0 ||
                       strcasecmp(title->b, "EXAMPLES") == 0 ||
                       strcasecmp(title->b, "DIAGNOSTICS") == 0 ||
                       strcasecmp(title->b, "ERRORS") == 0 ||
                       strcasecmp(title->b, "SEE ALSO") == 0 ||
                       strcasecmp(title->b, "STANDARDS") == 0 ||
                       strcasecmp(title->b, "HISTORY") == 0)
                           later = NULL;
                   else if ((strcasecmp(title->b, "CAVEATS") == 0 ||
                       strcasecmp(title->b, "BUGS") == 0) &&
                       later == NULL)
                           later = nc;
           }
   
         ptype = param = NULL;          /*
         TAILQ_FOREACH(pp, &pn->childq, child)           * If no AUTHORS section was found, create one from scratch,
                 if (NODE_TEXT == pp->node)           * and insert that at the place selected earlier.
                         ptype = pp;           */
                 else if (NODE_PARAMETER == pp->node)  
                         param = pp;  
   
         fputs(".Fa \"", stdout);          if (match == NULL) {
         if (NULL != ptype) {                  if ((match = calloc(1, sizeof(*match))) == NULL) {
                 pnode_printmacrolinepart(p, ptype);                          perror(NULL);
                 putchar(' ');                          exit(1);
                   }
                   match->node = NODE_SECTION;
                   match->spc = 1;
                   match->parent = n;
                   TAILQ_INIT(&match->childq);
                   TAILQ_INIT(&match->attrq);
                   if ((nc = pnode_alloc(match)) == NULL) {
                           perror(NULL);
                           exit(1);
                   }
                   nc->node = NODE_TITLE;
                   nc->spc = 1;
                   if ((nc = pnode_alloc(nc)) == NULL) {
                           perror(NULL);
                           exit(1);
                   }
                   nc->node = NODE_TEXT;
                   if ((nc->b = strdup("AUTHORS")) == NULL) {
                           perror(NULL);
                           exit(1);
                   }
                   nc->spc = 1;
                   if (later == NULL)
                           TAILQ_INSERT_TAIL(&n->childq, match, child);
                   else
                           TAILQ_INSERT_BEFORE(later, match, child);
         }          }
   
         if (NULL != param)          /*
                 pnode_printmacrolinepart(p, param);           * Dump the stuff excised at the beginning
         else           * into this AUTHORS section.
                 fputs("UNKNOWN", stdout);           */
   
         puts("\"");          if (info != NULL)
                   TAILQ_INSERT_TAIL(&match->childq, info, child);
           if (meta != NULL)
                   TAILQ_INSERT_TAIL(&match->childq, meta, child);
 }  }
   
   /*
    * We can have multiple <term> elements within a <varlistentry>, which
    * we should comma-separate as list headers.
    */
 static void  static void
 pnode_printfuncprototype(struct parse *p, struct pnode *pn)  pnode_printvarlistentry(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp, *fdef;          struct pnode    *nc, *nn, *ncc;
           int              comma;
   
         TAILQ_FOREACH(fdef, &pn->childq, child)          macro_open(f, "It");
                 if (NODE_FUNCDEF == fdef->node)          f->parastate = PARA_HAVE;
                         break;          f->flags |= FMT_IMPL;
           comma = -1;
           TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
                   if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM)
                           continue;
                   if (comma != -1) {
                           switch (f->linestate) {
                           case LINE_NEW:
                                   break;
                           case LINE_TEXT:
                                   print_text(f, ",", 0);
                                   break;
                           case LINE_MACRO:
                                   macro_addarg(f, ",", comma);
                                   break;
                           }
                   }
                   f->parastate = PARA_HAVE;
                   comma = (ncc = TAILQ_FIRST(&nc->childq)) == NULL ||
                       pnode_class(ncc->node) == CLASS_TEXT ? 0 : ARG_SPACE;
                   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;
   }
   
         if (NULL != fdef)  static void
                 pnode_printfuncdef(p, fdef);  pnode_printtitle(struct format *f, struct pnode *n)
         else  {
                 puts(".Fo UNKNOWN");          struct pnode    *nc, *nn;
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
                 if (NODE_PARAMDEF == pp->node)                  if (nc->node == NODE_TITLE) {
                         pnode_printparamdef(p, pp);                          if (f->parastate == PARA_MID)
                                   f->parastate = PARA_WANT;
                           macro_nodeline(f, "Sy", nc, 0);
                           pnode_unlink(nc);
                   }
           }
   }
   
         puts(".Fc");  static void
   pnode_printrow(struct format *f, struct pnode *n)
   {
           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);
 }  }
   
 /*  
  * 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").  
  * TODO: handle "optional" attribute.  
  */  
 static void  static void
 pnode_printarg(struct parse *p, struct pnode *pn)  pnode_printtgroup1(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc;
   
         TAILQ_FOREACH(pp, &pn->childq, child) {          macro_line(f, "Bl -bullet -compact");
                 if (NODE_TEXT == pp->node) {          while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) {
                         pnode_printmopen(p);                  macro_line(f, "It");
                         fputs("Ar ", stdout);                  f->parastate = PARA_HAVE;
                 }                  pnode_print(f, nc);
                 pnode_print(p, pp);                  f->parastate = PARA_HAVE;
                   pnode_unlink(nc);
         }          }
           macro_line(f, "El");
           pnode_unlinksub(n);
 }  }
   
 /*  static void
  * Recursively search and return the first instance of "node".  pnode_printtgroup2(struct format *f, struct pnode *n)
  */  
 static struct pnode *  
 pnode_findfirst(struct pnode *pn, enum nodeid node)  
 {  {
         struct pnode    *pp, *res;          struct pnode    *nr, *ne;
   
         res = NULL;          f->parastate = PARA_HAVE;
         TAILQ_FOREACH(pp, &pn->childq, child) {          macro_line(f, "Bl -tag -width Ds");
                 res = pp->node == node ? pp :          while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) {
                         pnode_findfirst(pp, node);                  if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL)
                 if (NULL != res)  
                         break;                          break;
                   macro_open(f, "It");
                   f->flags |= FMT_IMPL;
                   f->parastate = PARA_HAVE;
                   pnode_print(f, ne);
                   macro_close(f);
                   pnode_unlink(ne);
                   f->parastate = PARA_HAVE;
                   pnode_print(f, nr);
                   f->parastate = PARA_HAVE;
                   pnode_unlink(nr);
         }          }
           macro_line(f, "El");
           pnode_unlinksub(n);
   }
   
         return(res);  static void
   pnode_printtgroup(struct format *f, struct pnode *n)
   {
           struct pnode    *nc;
   
           switch (atoi(pnode_getattr_raw(n, ATTRKEY_COLS, "0"))) {
           case 1:
                   pnode_printtgroup1(f, n);
                   return;
           case 2:
                   pnode_printtgroup2(f, n);
                   return;
           default:
                   break;
           }
   
           f->parastate = PARA_HAVE;
           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  static void
 pnode_printprologue(struct parse *p, struct pnode *pn)  pnode_printlist(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc;
   
         pp = NULL == p->root ? NULL :          pnode_printtitle(f, n);
                 pnode_findfirst(p->root, NODE_REFMETA);          f->parastate = PARA_HAVE;
           macro_argline(f, "Bl",
               n->node == NODE_ORDEREDLIST ? "-enum" : "-bullet");
           TAILQ_FOREACH(nc, &n->childq, child) {
                   macro_line(f, "It");
                   f->parastate = PARA_HAVE;
                   pnode_print(f, nc);
                   f->parastate = PARA_HAVE;
           }
           macro_line(f, "El");
           pnode_unlinksub(n);
   }
   
         if (NULL != pp) {  static void
                 pnode_printrefmeta(p, pp);  pnode_printvariablelist(struct format *f, struct pnode *n)
                 pnode_unlink(pp);  {
         } else {          struct pnode    *nc;
                 puts(".\\\" Supplying bogus prologue...");  
                 puts(".Dd $Mdocdate" "$");          pnode_printtitle(f, n);
                 puts(".Dt UNKNOWN 1");          f->parastate = PARA_HAVE;
                 puts(".Os");          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, sv;  
   
         if (NULL == pn)          if (n == NULL)
                 return;                  return;
   
         sv = p->newln;          was_impl = f->flags & FMT_IMPL;
           if (n->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);                  pnode_printarg(f, n);
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CITEREFENTRY):          case NODE_AUTHOR:
                 assert(p->newln);                  pnode_printauthor(f, n);
                 pnode_printciterefentry(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CODE):          case NODE_AUTHORGROUP:
                 pnode_printmopen(p);                  macro_line(f, "An -split");
                 fputs("Li ", stdout);  
                 break;                  break;
         case (NODE_COMMAND):          case NODE_BLOCKQUOTE:
                 pnode_printmopen(p);                  f->parastate = PARA_HAVE;
                 fputs("Nm ", stdout);                  macro_line(f, "Bd -ragged -offset indent");
                   f->parastate = PARA_HAVE;
                 break;                  break;
         case (NODE_FUNCTION):          case NODE_CITEREFENTRY:
                 pnode_printmopen(p);                  pnode_printciterefentry(f, n);
                 fputs("Fn ", stdout);  
                 break;                  break;
         case (NODE_FUNCPROTOTYPE):          case NODE_CITETITLE:
                 assert(p->newln);                  macro_open(f, "%T");
                 pnode_printfuncprototype(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCSYNOPSISINFO):          case NODE_COMMAND:
                 pnode_printmopen(p);                  macro_open(f, "Nm");
                 fputs("Fd ", stdout);  
                 break;                  break;
         case (NODE_OPTION):          case NODE_CONSTANT:
                 pnode_printmopen(p);                  macro_open(f, "Dv");
                 fputs("Fl ", stdout);  
                 break;                  break;
         case (NODE_PARA):          case NODE_COPYRIGHT:
                 assert(p->newln);                  print_text(f, "Copyright", ARG_SPACE);
                 puts(".Pp");                  fputs(" \\(co", stdout);
                 break;                  break;
         case (NODE_PARAMETER):          case NODE_EDITOR:
                 /* Suppress non-text children... */                  print_text(f, "editor:", ARG_SPACE);
                 pnode_printmopen(p);                  pnode_printauthor(f, n);
                 fputs("Fa \"", stdout);  
                 pnode_printmacrolinepart(p, pn);  
                 puts("\"");  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_EMAIL:
                 assert(p->newln);                  if (was_impl)
                 puts(".Bd -literal");                          macro_open(f, "Ao Mt");
                   else {
                           macro_open(f, "Aq Mt");
                           f->flags |= FMT_IMPL;
                   }
                 break;                  break;
         case (NODE_REFMETA):          case NODE_EMPHASIS:
                 abort();          case NODE_FIRSTTERM:
           case NODE_GLOSSTERM:
                   if ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
                       pnode_class(nc->node) < CLASS_LINE)
                           macro_open(f, "Em");
                   if (n->node == NODE_GLOSSTERM)
                           f->parastate = PARA_HAVE;
                 break;                  break;
         case (NODE_REFNAME):          case NODE_ENVAR:
                 /* Suppress non-text children... */                  macro_open(f, "Ev");
                 pnode_printmopen(p);  
                 fputs("Nm ", stdout);  
                 pnode_printmacrolinepart(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_REFNAMEDIV):          case NODE_ERRORNAME:
                 assert(p->newln);                  macro_open(f, "Er");
                 puts(".Sh NAME");  
                 break;                  break;
         case (NODE_REFPURPOSE):          case NODE_FILENAME:
                 assert(p->newln);                  macro_open(f, "Pa");
                 fputs(".Nd ", stdout);  
                 break;                  break;
         case (NODE_REFSYNOPSISDIV):          case NODE_FOOTNOTE:
                 assert(p->newln);                  macro_line(f, "Bo");
                 pnode_printrefsynopsisdiv(p, pn);                  f->parastate = PARA_HAVE;
                 puts(".Sh SYNOPSIS");  
                 break;                  break;
         case (NODE_REFSECT1):          case NODE_FUNCTION:
                 assert(p->newln);                  macro_open(f, "Fn");
                 pnode_printrefsect(p, pn);  
                 break;                  break;
         case (NODE_STRUCTNAME):          case NODE_FUNCPROTOTYPE:
                 pnode_printmopen(p);                  pnode_printfuncprototype(f, n);
                 fputs("Vt ", stdout);  
                 break;                  break;
         case (NODE_TEXT):          case NODE_FUNCSYNOPSISINFO:
                 bufclear(p);                  macro_open(f, "Fd");
                 bufappend(p, pn);                  break;
                 /*          case NODE_IMAGEDATA:
                  * Output all characters, squeezing out whitespace                  pnode_printimagedata(f, n);
                  * between newlines.                  break;
                  * XXX: all whitespace, including tabs (?).          case NODE_INFORMALEQUATION:
                  * Remember to escape control characters and escapes.                  f->parastate = PARA_HAVE;
                  */                  macro_line(f, "Bd -ragged -offset indent");
                 assert(p->bsz);                  f->parastate = PARA_HAVE;
                 for (last = '\n', cp = p->b; '\0' != *cp; ) {                  /* FALLTHROUGH */
                         if ('\n' == last) {          case NODE_INLINEEQUATION:
                                 /* Consume all whitespace. */                  macro_line(f, "EQ");
                                 if (isspace((int)*cp)) {                  break;
                                         while (isspace((int)*cp))          case NODE_ITEMIZEDLIST:
                                                 cp++;                  pnode_printlist(f, n);
                                         continue;                  break;
                                 } else if ('\'' == *cp || '.' == *cp)          case NODE_GROUP:
                                         fputs("\\&", stdout);                  pnode_printgroup(f, n);
                         }                  break;
                         putchar(last = *cp++);          case NODE_KEYSYM:
                         /* If we're a character escape, escape us. */          case NODE_PRODUCTNAME:
                         if ('\\' == last)                  macro_open(f, "Sy");
                                 putchar('e');                  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;
                 }                  }
                 p->newln = 0;  
                 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->spc)
                                   nc->spc = 1;
                   } 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_REFENTRY:
                   pnode_printrefentry(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_PREFACE:
           case NODE_SECTION:
           case NODE_SIMPLESECT:
           case NODE_APPENDIX:
           case NODE_LEGALNOTICE:
           case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                   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->spc = 0;
                   break;
           case NODE_SUPERSCRIPT:
                   fputs("\\(ha", stdout);
                   if ((nc = TAILQ_FIRST(&n->childq)) != NULL)
                           nc->spc = 0;
                   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;
         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_ARG):          case NODE_EMAIL:
         case (NODE_CODE):                  if (was_impl) {
         case (NODE_COMMAND):                          f->flags &= ~FMT_NOSPC;
         case (NODE_FUNCTION):                          macro_open(f, "Ac");
         case (NODE_FUNCSYNOPSISINFO):                  } else
         case (NODE_OPTION):                          f->flags &= ~FMT_IMPL;
         case (NODE_PARAMETER):  
         case (NODE_REFNAME):  
         case (NODE_STRUCTNAME):  
         case (NODE_TEXT):  
                 pnode_printmclose(p, sv);  
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_ESCAPE:
                 assert(p->newln);          case NODE_TERM:
                 puts(".Ed");          case NODE_TEXT:
                 p->newln = 1;                  /* Accept more arguments to the previous macro. */
                   return;
           case NODE_FOOTNOTE:
                   f->parastate = PARA_HAVE;
                   macro_line(f, "Bc");
                 break;                  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_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:
                   f->parastate = PARA_HAVE;
                   macro_line(f, "Ed");
                   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.  
                  */  
                 p.newln = 1;  
                 pnode_printprologue(&p, p.root);  
                 pnode_print(&p, p.root);  
                 pnode_free(p.root);  
                 free(p.b);  
                 return(0 != rc && ! p.stop);  
         }  
   
         /* Read error has occured. */  
         perror(fn);  
         pnode_free(p.root);  
         free(p.b);  
         return(0);  
 }  
   
 int  
 main(int argc, char *argv[])  
 {  
         XML_Parser       xp;  
         const char      *fname;  
         char            *buf;  
         int              fd, rc;  
   
         fname = "-";  
         xp = NULL;  
         buf = NULL;  
         rc = 0;  
   
         if (-1 != getopt(argc, argv, ""))  
                 return(EXIT_FAILURE);  
   
         argc -= optind;  
         argv += optind;  
   
         if (argc > 1)  
                 return(EXIT_FAILURE);  
         else if (argc > 0)  
                 fname = argv[0];  
   
         /* Read from stdin or a file. */  
         fd = 0 == strcmp(fname, "-") ?  
                 STDIN_FILENO : open(fname, O_RDONLY, 0);  
   
         /*  
          * Open file for reading.  
          * Allocate a read buffer.  
          * Create the parser context.  
          * Dive directly into the parse.  
          */  
         if (-1 == fd)  
                 perror(fname);  
         else if (NULL == (buf = malloc(4096)))  
                 perror(NULL);  
         else if (NULL == (xp = XML_ParserCreate(NULL)))  
                 perror(NULL);  
         else if ( ! readfile(xp, fd, buf, 4096, fname))  
                 rc = 1;  
   
         XML_ParserFree(xp);  
         free(buf);  
         if (STDIN_FILENO != fd)  
                 close(fd);  
         return(rc ? EXIT_SUCCESS : EXIT_FAILURE);  
 }  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.134

CVSweb