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

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.12 and 1.80

version 1.12, 2014/03/29 22:44:06 version 1.80, 2019/04/02 16:24:23
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 <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  
 };  
   
 /*  /*
  * All recognised attribute keys.   * The implementation of the mdoc(7) formatter.
  */   */
 enum    attrkey {  
         /* Alpha-order... */  
         ATTRKEY_CHOICE = 0,  
         ATTRKEY_ID,  
         ATTRKEY_REP,  
         ATTRKEY__MAX  
 };  
   
 /*  static void      pnode_print(struct format *, struct pnode *);
  * All [explicitly] recognised attribute values.  
  * If an attribute has ATTRVAL__MAX, it could be a free-form.  
  */  
 enum    attrval {  
         /* Alpha-order... */  
         ATTRVAL_NOREPEAT,  
         ATTRVAL_OPT,  
         ATTRVAL_PLAIN,  
         ATTRVAL_REPEAT,  
         ATTRVAL_REQ,  
         ATTRVAL__MAX  
 };  
   
 /*  
  * Global parse state.  
  * Keep this as simple and small as possible.  
  */  
 struct  parse {  
         XML_Parser       xml;  
         enum nodeid      node; /* current (NODE_ROOT if pre-tree) */  
         const char      *fname; /* filename */  
         int              stop; /* should we stop now? */  
         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 {  
         const char      *name; /* docbook element name */  
         unsigned int     flags;  
 #define NODE_IGNTEXT     1 /* ignore all contained text */  
 };  
   
 TAILQ_HEAD(pnodeq, pnode);  
 TAILQ_HEAD(pattrq, pattr);  
   
 struct  pattr {  
         enum attrkey     key;  
         enum attrval     val;  
         char            *rawval;  
         TAILQ_ENTRY(pattr) child;  
 };  
   
 struct  pnode {  
         enum nodeid      node; /* node type */  
         char            *b; /* binary data buffer */  
         size_t           bsz; /* data buffer size */  
         struct pnode    *parent; /* parent (or NULL if top) */  
         struct pnodeq    childq; /* queue of children */  
         struct pattrq    attrq; /* attributes of node */  
         TAILQ_ENTRY(pnode) child;  
 };  
   
 static  const char *attrkeys[ATTRKEY__MAX] = {  
         "choice",  
         "id",  
         "rep"  
 };  
   
 static  const char *attrvals[ATTRVAL__MAX] = {  
         "norepeat",  
         "opt",  
         "plain",  
         "repeat",  
         "req"  
 };  
   
 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_printpara(struct format *p, struct pnode *pn)
   
 static int  
 isattrkey(enum nodeid node, enum attrkey key)  
 {  {
           struct pnode    *pp;
   
         switch (key) {          if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL &&
         case (ATTRKEY_CHOICE):              (pp = pn->parent) == NULL)
                 return(node == NODE_ARG);                  return;
         case (ATTRKEY_ID):  
                 /* Common to all. */          switch (pp->node) {
                 return(1);          case NODE_ENTRY:
         case (ATTRKEY_REP):          case NODE_LISTITEM:
                 return(node == NODE_ARG);                  return;
           case NODE_PREFACE:
           case NODE_SECTION:
                   if (p->level < 3)
                           return;
                   break;
         default:          default:
                 break;                  break;
         }          }
         abort();          macro_line(p, "Pp");
         return(0);  
 }  }
   
 static int  /*
 isattrval(enum attrkey key, enum attrval val)   * If the SYNOPSIS macro has a superfluous title, kill it.
    */
   static void
   pnode_printrefsynopsisdiv(struct format *p, struct pnode *pn)
 {  {
           struct pnode    *pp, *pq;
   
         switch (val) {          TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq)
         case (ATTRVAL_OPT):                  if (pp->node == NODE_TITLE)
         case (ATTRVAL_PLAIN):                          pnode_unlink(pp);
         case (ATTRVAL_REQ):  
                 return(key == ATTRKEY_CHOICE);          macro_line(p, "Sh SYNOPSIS");
         case (ATTRVAL_REPEAT):  
         case (ATTRVAL_NOREPEAT):  
                 return(key == ATTRKEY_REP);  
         default:  
                 break;  
         }  
         abort();  
         return(0);  
 }  }
   
 /*  /*
  * Look up whether "parent" is a valid parent for "node".   * Start a hopefully-named `Sh' section.
  * This is sucked directly from the DocBook specification: look at the  
  * "children" and "parent" sections of each node.  
  */   */
 static int  static void
 isparent(enum nodeid node, enum nodeid parent)  pnode_printrefsect(struct format *p, struct pnode *pn)
 {  {
           struct pnode    *pp;
           const char      *title;
           int              flags, level;
   
         switch (node) {          if (pn->parent == NULL)
         case (NODE_ROOT):                  return;
                 return(0);  
         case (NODE_ARG):          level = ++p->level;
                 switch (parent) {          flags = ARG_SPACE;
                 case (NODE_ARG):          if (level == 1)
                 case (NODE_CMDSYNOPSIS):                  flags |= ARG_UPPER;
                         return(1);          if (level < 3) {
                 default:                  switch (pn->node) {
                   case NODE_CAUTION:
                   case NODE_NOTE:
                   case NODE_TIP:
                   case NODE_WARNING:
                           level = 3;
                         break;                          break;
                 }  
                 return(0);  
         case (NODE_CITEREFENTRY):  
                 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_CMDSYNOPSIS):  
                 switch (parent) {          TAILQ_FOREACH(pp, &pn->childq, child)
                 case (NODE_PARA):                  if (pp->node == NODE_TITLE)
                 case (NODE_REFSECT1):  
                 case (NODE_REFSYNOPSISDIV):  
                         return(1);  
                 default:  
                         break;                          break;
                 }  
                 return(0);          if (pp == NULL) {
         case (NODE_CODE):                  switch (pn->node) {
                 switch (parent) {                  case NODE_PREFACE:
                 case (NODE_FUNCSYNOPSISINFO):                          title = "Preface";
                 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;                          break;
                 }                  case NODE_CAUTION:
                 return(0);                          title = "Caution";
         case (NODE_COMMAND):  
                 switch (parent) {  
                 case (NODE_CMDSYNOPSIS):  
                 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;                          break;
                 }                  case NODE_NOTE:
                 return(0);                          title = "Note";
         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;                          break;
                 }                  case NODE_TIP:
                 return(0);                          title = "Tip";
         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;                          break;
                 }                  case NODE_WARNING:
                 return(0);                          title = "Warning";
         case (NODE_MANVOLNUM):  
                 switch (parent) {  
                 case (NODE_CITEREFENTRY):  
                 case (NODE_REFMETA):  
                         return(1);  
                 default:  
                         break;                          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:                  default:
                           title = "Unknown";
                         break;                          break;
                 }                  }
                 return(0);          }
         case (NODE_PARA):  
                 switch (parent) {          switch (level) {
                 case (NODE_REFSECT1):          case 1:
                 case (NODE_REFSYNOPSISDIV):                  macro_open(p, "Sh");
                         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 2:
                   macro_open(p, "Ss");
                   break;
           default:
                   pnode_printpara(p, pn);
                   macro_open(p, "Sy");
                   break;
         }          }
   
         abort();          if (pp != NULL) {
         return(0);                  macro_addnode(p, pp, flags);
                   pnode_unlink(pp);
           } else
                   macro_addarg(p, title, ARG_SPACE | ARG_QUOTED);
           macro_close(p);
 }  }
   
 /*  /*
  * Process a stream of characters.   * Start a reference, extracting the title and volume.
  * 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_printciterefentry(struct format *p, struct pnode *pn)
 {  {
         struct parse    *ps = arg;          struct pnode    *pp, *title, *manvol;
         struct pnode    *dat;  
         int              i;  
   
         /* Stopped or no tree yet. */          title = manvol = NULL;
         if (ps->stop || NODE_ROOT == ps->node)          TAILQ_FOREACH(pp, &pn->childq, child) {
                 return;                  if (pp->node == NODE_MANVOLNUM)
                           manvol = pp;
         /* Not supposed to be collecting text. */                  else if (pp->node == NODE_REFENTRYTITLE)
         assert(NULL != ps->cur);                          title = pp;
         if (NODE_IGNTEXT & nodes[ps->node].flags)  
                 return;  
   
         /*  
          * Are we in the midst of processing text?  
          * If we're not processing text right now, then create a text  
          * node for doing so.  
          * However, don't do so unless we have some non-whitespace to  
          * process: strip out all leading whitespace to be sure.  
          */  
         if (NODE_TEXT != ps->node) {  
                 for (i = 0; i < sz; i++)  
                         if ( ! isspace((int)p[i]))  
                                 break;  
                 if (i == sz)  
                         return;  
                 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_INIT(&dat->attrq);  
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);  
                 ps->cur = dat;  
                 assert(NULL != ps->root);  
         }          }
           macro_open(p, "Xr");
         /* Append to current buffer. */          if (title == NULL)
         assert(sz >= 0);                  macro_addarg(p, "unknown", ARG_SPACE);
         ps->cur->b = realloc(ps->cur->b,          else
                 ps->cur->bsz + (size_t)sz);                  macro_addnode(p, title, ARG_SPACE | ARG_SINGLE);
         if (NULL == ps->cur->b) {          if (manvol == NULL)
                 perror(NULL);                  macro_addarg(p, "1", ARG_SPACE);
                 exit(EXIT_FAILURE);          else
         }                  macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE);
         memcpy(ps->cur->b + ps->cur->bsz, p, sz);          macro_close(p);
         ps->cur->bsz += (size_t)sz;          pnode_unlinksub(pn);
 }  }
   
 static void  static void
 pnode_trim(struct pnode *pn)  pnode_printrefmeta(struct format *p, struct pnode *pn)
 {  {
           struct pnode    *pp, *title, *manvol;
   
         assert(NODE_TEXT == pn->node);          title = manvol = NULL;
         for ( ; pn->bsz > 0; pn->bsz--)          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if ( ! isspace((int)pn->b[pn->bsz - 1]))                  if (pp->node == NODE_MANVOLNUM)
                         break;                          manvol = pp;
 }                  else if (pp->node == NODE_REFENTRYTITLE)
                           title = pp;
 /*  
  * Begin an element.  
  * First, look for the element.  
  * If we don't find it and we're not parsing, keep going.  
  * If we don't find it and we're parsing, puke and exit.  
  * If we find it but we're not parsing yet (i.e., it's not a refentry  
  * and thus out of context), keep going.  
  * If we find it and we're at the root and already have a tree, puke and  
  * exit (FIXME: I don't think this is right?).  
  * If we find it but we're parsing a text node, close out the text node,  
  * return to its parent, and keep going.  
  * Make sure that the element is in the right context.  
  * Lastly, put the node onto our parse tree and continue.  
  */  
 static void  
 xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts)  
 {  
         struct parse     *ps = arg;  
         enum nodeid       node;  
         enum attrkey      key;  
         enum attrval      val;  
         struct pnode     *dat;  
         struct pattr     *pattr;  
         const XML_Char  **att;  
   
         if (ps->stop)  
                 return;  
   
         /* Close out text node, if applicable... */  
         if (NODE_TEXT == ps->node) {  
                 assert(NULL != ps->cur);  
                 pnode_trim(ps->cur);  
                 ps->cur = ps->cur->parent;  
                 assert(NULL != ps->cur);  
                 ps->node = ps->cur->node;  
         }          }
           macro_open(p, "Dt");
         for (node = 0; node < NODE__MAX; node++)          if (title == NULL)
                 if (NULL == nodes[node].name)                  macro_addarg(p, "UNKNOWN", ARG_SPACE);
                         continue;          else
                 else if (0 == strcmp(nodes[node].name, name))                  macro_addnode(p, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER);
                         break;          if (manvol == NULL)
                   macro_addarg(p, "1", ARG_SPACE);
         if (NODE__MAX == node && NODE_ROOT == ps->node) {          else
                 return;                  macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE);
         } else if (NODE__MAX == node) {          macro_close(p);
                 fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n",          pnode_unlink(pn);
                         ps->fname, XML_GetCurrentLineNumber(ps->xml),  
                         XML_GetCurrentColumnNumber(ps->xml), name);  
                 ps->stop = 1;  
                 return;  
         } else if (NODE_ROOT == ps->node && NULL != ps->root) {  
                 fprintf(stderr, "%s:%zu:%zu: multiple refentries\n",  
                         ps->fname, XML_GetCurrentLineNumber(ps->xml),  
                         XML_GetCurrentColumnNumber(ps->xml));  
                 ps->stop = 1;  
                 return;  
         } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {  
                 return;  
         } else if ( ! isparent(node, ps->node)) {  
                 fprintf(stderr, "%s:%zu:%zu: bad parent \"%s\"\n",  
                         ps->fname, XML_GetCurrentLineNumber(ps->xml),  
                         XML_GetCurrentColumnNumber(ps->xml),  
                         NULL == nodes[ps->node].name ?  
                         "(none)" : nodes[ps->node].name);  
                 ps->stop = 1;  
                 return;  
         }  
   
         if (NULL == (dat = calloc(1, sizeof(struct pnode)))) {  
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
   
         dat->node = ps->node = node;  
         dat->parent = ps->cur;  
         TAILQ_INIT(&dat->childq);  
         TAILQ_INIT(&dat->attrq);  
   
         if (NULL != ps->cur)  
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);  
   
         ps->cur = dat;  
         if (NULL == ps->root)  
                 ps->root = dat;  
   
         /*  
          * Process attributes.  
          */  
         for (att = atts; NULL != *att; att += 2) {  
                 for (key = 0; key < ATTRKEY__MAX; key++)  
                         if (0 == strcmp(*att, attrkeys[key]))  
                                 break;  
                 if (ATTRKEY__MAX == key) {  
                         fprintf(stderr, "%s:%zu:%zu: unknown "  
                                 "attribute \"%s\"\n", ps->fname,  
                                 XML_GetCurrentLineNumber(ps->xml),  
                                 XML_GetCurrentColumnNumber(ps->xml),  
                                 *att);  
                         continue;  
                 } else if ( ! isattrkey(node, key)) {  
                         fprintf(stderr, "%s:%zu:%zu: bad "  
                                 "attribute \"%s\"\n", ps->fname,  
                                 XML_GetCurrentLineNumber(ps->xml),  
                                 XML_GetCurrentColumnNumber(ps->xml),  
                                 *att);  
                         continue;  
                 }  
                 for (val = 0; val < ATTRVAL__MAX; val++)  
                         if (0 == strcmp(*(att + 1), attrvals[val]))  
                                 break;  
                 if (ATTRVAL__MAX != val && ! isattrval(key, val)) {  
                         fprintf(stderr, "%s:%zu:%zu: bad "  
                                 "value \"%s\"\n", ps->fname,  
                                 XML_GetCurrentLineNumber(ps->xml),  
                                 XML_GetCurrentColumnNumber(ps->xml),  
                                 *(att + 1));  
                         continue;  
                 }  
                 pattr = calloc(1, sizeof(struct pattr));  
                 pattr->key = key;  
                 pattr->val = val;  
                 if (ATTRVAL__MAX == val)  
                         pattr->rawval = strdup(*(att + 1));  
                 TAILQ_INSERT_TAIL(&dat->attrq, pattr, child);  
         }  
   
 }  }
   
 /*  
  * Roll up the parse tree.  
  * If we're at a text node, roll that one up first.  
  * If we hit the root, then assign ourselves as the NODE_ROOT.  
  */  
 static void  static void
 xml_elem_end(void *arg, const XML_Char *name)  pnode_printfuncdef(struct format *p, struct pnode *pn)
 {  {
         struct parse    *ps = arg;          struct pnode    *pp, *ftype, *func;
   
         if (ps->stop || NODE_ROOT == ps->node)          ftype = func = NULL;
                 return;          TAILQ_FOREACH(pp, &pn->childq, child) {
                   if (pp->node == NODE_TEXT)
         /* Close out text node, if applicable... */                          ftype = pp;
         if (NODE_TEXT == ps->node) {                  else if (pp->node == NODE_FUNCTION)
                 assert(NULL != ps->cur);                          func = pp;
                 pnode_trim(ps->cur);  
                 ps->cur = ps->cur->parent;  
                 assert(NULL != ps->cur);  
                 ps->node = ps->cur->node;  
         }          }
           if (ftype != NULL)
         if (NULL == (ps->cur = ps->cur->parent))                  macro_argline(p, "Ft", ftype->b);
                 ps->node = NODE_ROOT;          macro_open(p, "Fo");
           if (func == NULL)
                   macro_addarg(p, "UNKNOWN", ARG_SPACE);
         else          else
                 ps->node = ps->cur->node;                  macro_addnode(p, func, ARG_SPACE | ARG_SINGLE);
           macro_close(p);
 }  }
   
 /*  /*
  * 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 *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
         struct pattr    *ap;  
   
         if (NULL == pn)          printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "("));
                 return;  
   
         while (NULL != (pp = TAILQ_FIRST(&pn->childq))) {          pp = TAILQ_FIRST(&pn->childq);
                 TAILQ_REMOVE(&pn->childq, pp, child);          pnode_print(p, pp);
                 pnode_free(pp);  
         }  
   
         while (NULL != (ap = TAILQ_FIRST(&pn->attrq))) {          while ((pp = TAILQ_NEXT(pp, child)) != NULL) {
                 TAILQ_REMOVE(&pn->attrq, ap, child);                  putchar(',');
                 free(ap->rawval);                  pnode_print(p, pp);
                 free(ap);  
         }          }
           printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")"));
         free(pn->b);          pnode_unlinksub(pn);
         free(pn);  
 }  }
   
 /*  /*
  * 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 *p, struct pnode *pn)
 {  {
           struct pnode    *pp;
   
         if (NULL != pn->parent)          pp = TAILQ_FIRST(&pn->childq);
                 TAILQ_REMOVE(&pn->parent->childq, pn, child);          pnode_print(p, pp);
         pnode_free(pn);  
 }  
   
 /*          switch (pn->node) {
  * Unlink all children of a node and pnode_free() them.          case NODE_MML_MSUP:
  */                  fputs(" sup ", stdout);
 static void                  break;
 pnode_unlinksub(struct pnode *pn)          case NODE_MML_MFRAC:
 {                  fputs(" over ", stdout);
                   break;
           case NODE_MML_MSUB:
                   fputs(" sub ", stdout);
                   break;
           default:
                   break;
           }
   
         while ( ! TAILQ_EMPTY(&pn->childq))          pp = TAILQ_NEXT(pp, child);
                 pnode_unlink(TAILQ_FIRST(&pn->childq));          pnode_print(p, pp);
           pnode_unlinksub(pn);
 }  }
   
 /*  
  * Reset the lookaside buffer.  
  */  
 static void  static void
 bufclear(struct parse *p)  pnode_printfuncprototype(struct format *p, struct pnode *pn)
 {  {
           struct pnode    *pp, *fdef;
   
         p->b[p->bsz = 0] = '\0';          TAILQ_FOREACH(fdef, &pn->childq, child)
 }                  if (fdef->node == NODE_FUNCDEF)
                           break;
   
 /*          if (fdef != NULL)
  * Append NODE_TEXT contents to the current buffer, reallocating its                  pnode_printfuncdef(p, fdef);
  * size if necessary.          else
  * The buffer is ALWAYS nil-terminated.                  macro_line(p, "Fo UNKNOWN");
  */  
 static void  
 bufappend(struct parse *p, struct pnode *pn)  
 {  
   
         assert(NODE_TEXT == pn->node);          TAILQ_FOREACH(pp, &pn->childq, child)
         if (p->bsz + pn->bsz + 1 > p->mbsz) {                  if (pp->node == NODE_PARAMDEF)
                 p->mbsz = p->bsz + pn->bsz + 1;                          macro_nodeline(p, "Fa", pp, ARG_SINGLE);
                 if (NULL == (p->b = realloc(p->b, p->mbsz))) {  
                         perror(NULL);          macro_line(p, "Fc");
                         exit(EXIT_FAILURE);          pnode_unlinksub(pn);
                 }  
         }  
         memcpy(p->b + p->bsz, pn->b, pn->bsz);  
         p->bsz += pn->bsz;  
         p->b[p->bsz] = '\0';  
 }  }
   
 /*  /*
  * Recursively append all NODE_TEXT nodes to the buffer.   * The <arg> element is more complicated than it should be because text
  * This descends into non-text nodes, but doesn't do anything beyond   * nodes are treated like ".Ar foo", but non-text nodes need to be
  * them.   * re-sent into the printer (i.e., without the preceding ".Ar").
  * In other words, this is a recursive text grok.   * This also handles the case of "repetition" (or in other words, the
    * ellipsis following an argument) and optionality.
  */   */
 static void  static void
 bufappend_r(struct parse *p, struct pnode *pn)  pnode_printarg(struct format *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           struct pattr    *ap;
           int              isop, isrep;
   
         if (NODE_TEXT == pn->node)          isop = 1;
                 bufappend(p, pn);          isrep = 0;
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(ap, &pn->attrq, child) {
                 bufappend_r(p, pp);                  if (ap->key == ATTRKEY_CHOICE &&
 }                      (ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ))
                           isop = 0;
                   else if (ap->key == ATTRKEY_REP && ap->val == ATTRVAL_REPEAT)
                           isrep = 1;
           }
           if (isop)
                   macro_open(p, "Op");
   
 #define MACROLINE_NORM  0          TAILQ_FOREACH(pp, &pn->childq, child) {
 #define MACROLINE_UPPER 1                  if (pp->node == NODE_TEXT)
 /*                          macro_open(p, "Ar");
  * Recursively print text presumably on a macro line.                  pnode_print(p, pp);
  * Convert all whitespace to regular spaces.                  if (isrep && pp->node == NODE_TEXT)
  */                          macro_addarg(p, "...", ARG_SPACE);
 static void  
 pnode_printmacrolinetext(struct parse *p, struct pnode *pn, int fl)  
 {  
         char            *cp;  
   
         bufclear(p);  
         bufappend_r(p, pn);  
   
         /* Convert all space to spaces. */  
         for (cp = p->b; '\0' != *cp; cp++)  
                 if (isspace((int)*cp))  
                         *cp = ' ';  
   
         for (cp = p->b; isspace((int)*cp); cp++)  
                 /* Spin past whitespace (XXX: necessary?) */ ;  
         for ( ; '\0' != *cp; cp++) {  
                 /* Escape us if we look like a macro. */  
                 if ((cp == p->b || ' ' == *(cp - 1)) &&  
                         isupper((int)*cp) &&  
                         '\0' != *(cp + 1) &&  
                         islower((int)*(cp + 1)) &&  
                         ('\0' == *(cp + 2) ||  
                          ' ' == *(cp + 2) ||  
                          (islower((int)*(cp + 2)) &&  
                           ('\0' == *(cp + 3) ||  
                            ' ' == *(cp + 3)))))  
                         fputs("\\&", stdout);  
                 if (MACROLINE_UPPER & fl)  
                         putchar(toupper((int)*cp));  
                 else  
                         putchar((int)*cp);  
                 /* If we're a character escape, escape us. */  
                 if ('\\' == *cp)  
                         putchar('e');  
         }          }
           pnode_unlinksub(pn);
 }  }
   
 static void  static void
 pnode_printmacrolinepart(struct parse *p, struct pnode *pn)  pnode_printgroup(struct format *p, struct pnode *pn)
 {  {
           struct pnode    *pp, *np;
           struct pattr    *ap;
           int              isop, sv;
   
         pnode_printmacrolinetext(p, pn, 0);          isop = 1;
 }          TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ap->key == ATTRKEY_CHOICE &&
                       (ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ)) {
                           isop = 0;
                           break;
                   }
   
 /*          /*
  * Just pnode_printmacrolinepart() but with a newline.           * Make sure we're on a macro line.
  * If no text, just the newline.           * This will prevent pnode_print() for putting us on a
  */           * subsequent line.
 static void           */
 pnode_printmacroline(struct parse *p, struct pnode *pn)          sv = p->linestate == LINE_NEW;
 {          if (isop)
                   macro_open(p, "Op");
           else if (sv)
                   macro_open(p, "No");
   
         pnode_printmacrolinetext(p, pn, 0);          /*
         putchar('\n');           * Keep on printing text separated by the vertical bar as long
            * as we're within the same origin node as the group.
            * This is kind of a nightmare.
            * Eh, DocBook...
            * FIXME: if there's a "Fl", we don't cut off the leading "-"
            * like we do in pnode_print().
            */
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   pnode_print(p, pp);
                   np = TAILQ_NEXT(pp, child);
                   while (np != NULL) {
                           if (pp->node != np->node)
                                   break;
                           macro_addarg(p, "|", ARG_SPACE);
                           macro_addnode(p, np, ARG_SPACE);
                           pp = np;
                           np = TAILQ_NEXT(np, child);
                   }
           }
           if (sv)
                   macro_close(p);
           pnode_unlinksub(pn);
 }  }
   
 static void  static void
 pnode_printmopen(struct parse *p)  pnode_printauthor(struct format *f, struct pnode *n)
 {  {
         if (p->newln) {          struct pnode    *nc, *ncn;
                 putchar('.');          int              have_contrib, have_name;
                 p->newln = 0;  
         } else  
                 putchar(' ');  
 }  
   
 static void          /*
 pnode_printmclose(struct parse *p, int sv)           * Print <contrib> children up front, before the .An scope,
 {           * and figure out whether we a name of a person.
            */
   
         if (sv && ! p->newln) {          have_contrib = have_name = 0;
                 putchar('\n');          TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) {
                 p->newln = 1;                  switch (nc->node) {
                   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;
   
 /*          if (have_contrib)
  * If the SYNOPSIS macro has a superfluous title, kill it.                  print_text(f, ":", 0);
  */  
 static void  
 pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn)  
 {  
         struct pnode    *pp;  
   
         TAILQ_FOREACH(pp, &pn->childq, child)          /*
                 if (NODE_TITLE == pp->node) {           * If we have a name, print it in the .An scope and leave
                         pnode_unlink(pp);           * all other content for child handlers, to print after the
                         return;           * scope.  Otherwise, print everything in the scope.
            */
   
           macro_open(f, "An");
           TAILQ_FOREACH_SAFE(nc, &n->childq, child, ncn) {
                   if (nc->node == NODE_PERSONNAME || have_name == 0) {
                           macro_addnode(f, nc, ARG_SPACE);
                           pnode_unlink(nc);
                 }                  }
 }          }
   
 /*          /*
  * Start a hopefully-named `Sh' section.           * If there is an email address,
  */           * print it on the same macro line.
 static void           */
 pnode_printrefsect(struct parse *p, struct pnode *pn)  
 {  
         struct pnode    *pp;  
   
         TAILQ_FOREACH(pp, &pn->childq, child)          if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) {
                 if (NODE_TITLE == pp->node)                  pnode_print(f, nc);
                         break;                  pnode_unlink(nc);
           }
   
         fputs(".Sh ", stdout);          /*
            * 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 (NULL != pp) {          if (TAILQ_FIRST(&n->childq) != NULL) {
                 pnode_printmacroline(p, pp);                  macro_addarg(f, ",", ARG_SPACE);
                 pnode_unlink(pp);                  macro_close(f);
         } else          }
                 puts("UNKNOWN");  
 }  }
   
 /*  
  * Start a reference, extracting the title and volume.  
  */  
 static void  static void
 pnode_printciterefentry(struct parse *p, struct pnode *pn)  pnode_printprologue(struct format *p, struct ptree *tree)
 {  {
         struct pnode    *pp, *title, *manvol;          struct pnode    *refmeta;
   
         title = manvol = NULL;          refmeta = tree->root == NULL ? NULL :
         TAILQ_FOREACH(pp, &pn->childq, child)              pnode_findfirst(tree->root, NODE_REFMETA);
                 if (NODE_MANVOLNUM == pp->node)  
                         manvol = pp;  
                 else if (NODE_REFENTRYTITLE == pp->node)  
                         title = pp;  
   
         fputs(".Xr ", stdout);          macro_line(p, "Dd $Mdocdate" "$");
           if (refmeta == NULL) {
         if (NULL != title) {                  macro_open(p, "Dt");
                 pnode_printmacrolinepart(p, title);                  macro_addarg(p,
                 putchar(' ');                      pnode_getattr_raw(tree->root, ATTRKEY_ID, "UNKNOWN"),
                       ARG_SPACE | ARG_SINGLE | ARG_UPPER);
                   macro_addarg(p, "1", ARG_SPACE);
                   macro_close(p);
         } else          } else
                 fputs("unknown ", stdout);                  pnode_printrefmeta(p, refmeta);
           macro_line(p, "Os");
   
         if (NULL != manvol)          if (tree->flags & TREE_EQN) {
                 pnode_printmacroline(p, manvol);                  macro_line(p, "EQ");
         else                  print_text(p, "delim $$", 0);
                 puts("1");                  macro_line(p, "EN");
           }
 }  }
   
   /*
    * 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 *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *title, *manvol;          struct pnode    *pp;
           int              first = 1;
   
         title = manvol = NULL;          macro_open(p, "It");
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   if (pp->node != NODE_TERM)
                           continue;
                   if ( ! first)
                           macro_addarg(p, ",", 0);
                   pnode_print(p, pp);
                   first = 0;
           }
           macro_close(p);
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_MANVOLNUM == pp->node)                  if (pp->node != NODE_TERM)
                         manvol = pp;                          pnode_print(p, pp);
                 else if (NODE_REFENTRYTITLE == pp->node)          pnode_unlinksub(pn);
                         title = pp;  
   
         puts(".Dd $Mdocdate" "$");  
         fputs(".Dt ", stdout);  
   
         if (NULL != title) {  
                 /* FIXME: uppercase. */  
                 pnode_printmacrolinetext(p, title, MACROLINE_UPPER);  
                 putchar(' ');  
         } else  
                 fputs("UNKNOWN ", stdout);  
   
         if (NULL != manvol)  
                 pnode_printmacroline(p, manvol);  
         else  
                 puts("1");  
   
         puts(".Os");  
 }  }
   
 static void  static void
 pnode_printfuncdef(struct parse *p, struct pnode *pn)  pnode_printtitle(struct format *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *ftype, *func;          struct pnode    *pp, *pq;
   
         ftype = func = NULL;          TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) {
         TAILQ_FOREACH(pp, &pn->childq, child)                  if (pp->node == NODE_TITLE) {
                 if (NODE_TEXT == pp->node)                          pnode_printpara(p, pp);
                         ftype = pp;                          pnode_print(p, pp);
                 else if (NODE_FUNCTION == pp->node)                          pnode_unlink(pp);
                         func = pp;                  }
   
         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 *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *ptype, *param;          struct pnode    *pp;
   
         ptype = param = NULL;          macro_line(p, "Bl -dash -compact");
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_TEXT == pp->node)                  macro_line(p, "It");
                         ptype = pp;                  pnode_print(p, pp);
                 else if (NODE_PARAMETER == pp->node)  
                         param = pp;  
   
         fputs(".Fa \"", stdout);  
         if (NULL != ptype) {  
                 pnode_printmacrolinepart(p, ptype);  
                 putchar(' ');  
         }          }
           macro_line(p, "El");
         if (NULL != param)          pnode_unlink(pn);
                 pnode_printmacrolinepart(p, param);  
   
         puts("\"");  
 }  }
   
 static void  static void
 pnode_printfuncprototype(struct parse *p, struct pnode *pn)  pnode_printtable(struct format *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *fdef;          struct pnode    *pp;
   
         TAILQ_FOREACH(fdef, &pn->childq, child)          pnode_printtitle(p, pn);
                 if (NODE_FUNCDEF == fdef->node)          macro_line(p, "Bl -ohang");
                         break;          while ((pp = pnode_findfirst(pn, NODE_ROW)) != NULL) {
                   macro_line(p, "It Table Row");
         if (NULL != fdef)                  pnode_printrow(p, pp);
                 pnode_printfuncdef(p, fdef);          }
         else          macro_line(p, "El");
                 puts(".Fo UNKNOWN");          pnode_unlinksub(pn);
   
         TAILQ_FOREACH(pp, &pn->childq, child)  
                 if (NODE_PARAMDEF == pp->node)  
                         pnode_printparamdef(p, pp);  
   
         puts(".Fc");  
 }  }
   
 /*  
  * 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
 pnode_printarg(struct parse *p, struct pnode *pn)  pnode_printlist(struct format *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
         struct pattr    *ap;  
         int              isop, isrep;  
   
         isop = 1;          pnode_printtitle(p, pn);
         isrep = 0;          macro_argline(p, "Bl",
         TAILQ_FOREACH(ap, &pn->attrq, child)              pn->node == NODE_ORDEREDLIST ? "-enum" : "-bullet");
                 if (ATTRKEY_CHOICE == ap->key &&  
                         (ATTRVAL_PLAIN == ap->val ||  
                          ATTRVAL_REQ == ap->val))  
                         isop = 0;  
                 else if (ATTRKEY_REP == ap->key &&  
                         (ATTRVAL_REPEAT == ap->val))  
                         isrep = 1;  
   
         if (isop) {  
                 pnode_printmopen(p);  
                 fputs("Op ", stdout);  
         }  
   
         TAILQ_FOREACH(pp, &pn->childq, child) {          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_TEXT == pp->node) {                  macro_line(p, "It");
                         pnode_printmopen(p);  
                         fputs("Ar ", stdout);  
                 }  
                 pnode_print(p, pp);                  pnode_print(p, pp);
                 if (NODE_TEXT == pp->node && isrep)  
                         fputs("...", stdout);  
         }          }
           macro_line(p, "El");
           pnode_unlinksub(pn);
 }  }
   
 /*  
  * Recursively search and return the first instance of "node".  
  */  
 static struct pnode *  
 pnode_findfirst(struct pnode *pn, enum nodeid node)  
 {  
         struct pnode    *pp, *res;  
   
         res = NULL;  
         TAILQ_FOREACH(pp, &pn->childq, child) {  
                 res = pp->node == node ? pp :  
                         pnode_findfirst(pp, node);  
                 if (NULL != res)  
                         break;  
         }  
   
         return(res);  
 }  
   
 static void  static void
 pnode_printprologue(struct parse *p, struct pnode *pn)  pnode_printvariablelist(struct format *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
   
         pp = NULL == p->root ? NULL :          pnode_printtitle(p, pn);
                 pnode_findfirst(p->root, NODE_REFMETA);          macro_line(p, "Bl -tag -width Ds");
           TAILQ_FOREACH(pp, &pn->childq, child) {
         if (NULL != pp) {                  if (pp->node == NODE_VARLISTENTRY)
                 pnode_printrefmeta(p, pp);                          pnode_print(p, pp);
                 pnode_unlink(pp);                  else
         } else {                          macro_nodeline(p, "It", pp, 0);
                 puts(".\\\" Supplying bogus prologue...");  
                 puts(".Dd $Mdocdate" "$");  
                 puts(".Dt UNKNOWN 1");  
                 puts(".Os");  
         }          }
           macro_line(p, "El");
           pnode_unlinksub(pn);
 }  }
   
 /*  /*
  * 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 *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           const char      *ccp;
         char            *cp;          char            *cp;
         int              last, sv;          int              last;
           enum linestate   sv;
   
         if (NULL == pn)          if (pn == NULL)
                 return;                  return;
   
         sv = p->newln;          sv = p->linestate;
   
         switch (pn->node) {          switch (pn->node) {
         case (NODE_ARG):          case NODE_APPLICATION:
                   macro_open(p, "Nm");
                   break;
           case NODE_ARG:
                 pnode_printarg(p, pn);                  pnode_printarg(p, pn);
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CITEREFENTRY):          case NODE_AUTHOR:
                 assert(p->newln);                  pnode_printauthor(p, pn);
                   break;
           case NODE_AUTHORGROUP:
                   macro_line(p, "An -split");
                   break;
           case NODE_BOOKINFO:
                   macro_line(p, "Sh NAME");
                   break;
           case NODE_CITEREFENTRY:
                 pnode_printciterefentry(p, pn);                  pnode_printciterefentry(p, pn);
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CODE):          case NODE_CITETITLE:
                 pnode_printmopen(p);                  macro_open(p, "%T");
                 fputs("Li ", stdout);  
                 break;                  break;
         case (NODE_COMMAND):          case NODE_CODE:
                 pnode_printmopen(p);                  macro_open(p, "Li");
                 fputs("Nm ", stdout);  
                 break;                  break;
         case (NODE_FUNCTION):          case NODE_COMMAND:
                 pnode_printmopen(p);                  macro_open(p, "Nm");
                 fputs("Fn ", stdout);  
                 break;                  break;
         case (NODE_FUNCPROTOTYPE):          case NODE_CONSTANT:
                 assert(p->newln);                  macro_open(p, "Dv");
                   break;
           case NODE_EDITOR:
                   print_text(p, "editor:", ARG_SPACE);
                   macro_open(p, "An");
                   break;
           case NODE_EMAIL:
                   macro_open(p, "Aq Mt");
                   break;
           case NODE_EMPHASIS:
           case NODE_FIRSTTERM:
                   macro_open(p, "Em");
                   break;
           case NODE_ENVAR:
                   macro_open(p, "Ev");
                   break;
           case NODE_ESCAPE:
                   if (p->linestate == LINE_NEW)
                           p->linestate = LINE_TEXT;
                   else
                           putchar(' ');
                   fputs(pn->b, stdout);
                   break;
           case NODE_FILENAME:
                   macro_open(p, "Pa");
                   break;
           case NODE_FUNCTION:
                   macro_open(p, "Fn");
                   break;
           case NODE_FUNCPROTOTYPE:
                 pnode_printfuncprototype(p, pn);                  pnode_printfuncprototype(p, pn);
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCSYNOPSISINFO):          case NODE_FUNCSYNOPSISINFO:
                 pnode_printmopen(p);                  macro_open(p, "Fd");
                 fputs("Fd ", stdout);  
                 break;                  break;
         case (NODE_OPTION):          case NODE_INFORMALEQUATION:
                 pnode_printmopen(p);                  macro_line(p, "EQ");
                 fputs("Fl ", stdout);  
                 break;                  break;
         case (NODE_PARA):          case NODE_INLINEEQUATION:
                 assert(p->newln);                  if (p->linestate == LINE_NEW)
                 puts(".Pp");                          p->linestate = LINE_TEXT;
                   putchar('$');
                 break;                  break;
         case (NODE_PARAMETER):          case NODE_ITEMIZEDLIST:
                 /* Suppress non-text children... */                  pnode_printlist(p, pn);
                 pnode_printmopen(p);                  break;
                 fputs("Fa \"", stdout);          case NODE_GROUP:
                 pnode_printmacrolinepart(p, pn);                  pnode_printgroup(p, pn);
                 puts("\"");                  break;
           case NODE_KEYSYM:
                   macro_open(p, "Sy");
                   break;
           case NODE_LEGALNOTICE:
                   macro_line(p, "Sh LEGAL NOTICE");
                   break;
           case NODE_LINK:
                   ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL);
                   if (ccp == NULL)
                           break;
                   macro_argline(p, "Sx", ccp);
                   return;
           case NODE_LITERAL:
                   macro_open(p, "Li");
                   break;
           case NODE_LITERALLAYOUT:
                   macro_argline(p, "Bd", pnode_getattr(pn, ATTRKEY_CLASS) ==
                       ATTRVAL_MONOSPACED ? "-literal" : "-unfilled");
                   break;
           case NODE_MML_MFENCED:
                   pnode_printmathfenced(p, pn);
                   break;
           case NODE_MML_MROW:
           case NODE_MML_MI:
           case NODE_MML_MN:
           case NODE_MML_MO:
                   if (TAILQ_EMPTY(&pn->childq))
                           break;
                   fputs(" { ", stdout);
                   break;
           case NODE_MML_MFRAC:
           case NODE_MML_MSUB:
           case NODE_MML_MSUP:
                   pnode_printmath(p, pn);
                   break;
           case NODE_OPTION:
                   macro_open(p, "Fl");
                   break;
           case NODE_ORDEREDLIST:
                   pnode_printlist(p, pn);
                   break;
           case NODE_PARA:
                   pnode_printpara(p, pn);
                   break;
           case NODE_PARAMETER:
                   macro_nodeline(p, "Fa", pn, ARG_SINGLE);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_QUOTE:
                 assert(p->newln);                  macro_open(p, "Qo");
                 puts(".Bd -literal");  
                 break;                  break;
         case (NODE_REFMETA):          case NODE_PROGRAMLISTING:
           case NODE_SCREEN:
                   macro_line(p, "Bd -literal");
                   break;
           case NODE_REFENTRYINFO:
                   /* Suppress. */
                   pnode_unlinksub(pn);
                   break;
           case NODE_REFMETA:
                 abort();                  abort();
                 break;                  break;
         case (NODE_REFNAME):          case NODE_REFNAME:
                 /* Suppress non-text children... */                  /* Suppress non-text children... */
                 pnode_printmopen(p);                  macro_open(p, "Nm");
                 fputs("Nm ", stdout);                  macro_addnode(p, pn, ARG_SPACE | ARG_SINGLE);
                 pnode_printmacrolinepart(p, pn);  
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_REFNAMEDIV):          case NODE_REFNAMEDIV:
                 assert(p->newln);                  macro_line(p, "Sh NAME");
                 puts(".Sh NAME");  
                 break;                  break;
         case (NODE_REFPURPOSE):          case NODE_REFPURPOSE:
                 assert(p->newln);                  macro_open(p, "Nd");
                 fputs(".Nd ", stdout);  
                 break;                  break;
         case (NODE_REFSYNOPSISDIV):          case NODE_REFSYNOPSISDIV:
                 assert(p->newln);  
                 pnode_printrefsynopsisdiv(p, pn);                  pnode_printrefsynopsisdiv(p, pn);
                 puts(".Sh SYNOPSIS");  
                 break;                  break;
         case (NODE_REFSECT1):          case NODE_PREFACE:
                 assert(p->newln);          case NODE_SECTION:
           case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                 pnode_printrefsect(p, pn);                  pnode_printrefsect(p, pn);
                 break;                  break;
         case (NODE_STRUCTNAME):          case NODE_REPLACEABLE:
                 pnode_printmopen(p);                  macro_open(p, "Ar");
                 fputs("Vt ", stdout);  
                 break;                  break;
         case (NODE_TEXT):          case NODE_SBR:
                 bufclear(p);                  macro_line(p, "br");
                 bufappend(p, pn);                  break;
           case NODE_SGMLTAG:
                   macro_open(p, "Li");
                   break;
           case NODE_STRUCTNAME:
                   macro_open(p, "Vt");
                   break;
           case NODE_TABLE:
           case NODE_INFORMALTABLE:
                   pnode_printtable(p, pn);
                   break;
           case NODE_TEXT:
                   if (pn->bsz == 0) {
                           assert(pn->real != pn->b);
                           break;
                   }
                   if (p->linestate == LINE_NEW)
                           p->linestate = LINE_TEXT;
                   else
                           putchar(' ');
   
                 /*                  /*
                  * Output all characters, squeezing out whitespace                   * Output all characters, squeezing out whitespace
                  * between newlines.                   * between newlines.
                  * XXX: all whitespace, including tabs (?).                   * XXX: all whitespace, including tabs (?).
                  * Remember to escape control characters and escapes.                   * Remember to escape control characters and escapes.
                  */                   */
                 assert(p->bsz);                  cp = pn->b;
                 for (last = '\n', cp = p->b; '\0' != *cp; ) {  
                         if ('\n' == last) {                  /*
                    * There's often a superfluous "-" in its <option> tags
                    * before the actual flags themselves.
                    * "Fl" does this for us, so remove it.
                    */
                   if (pn->parent != NULL &&
                       pn->parent->node == NODE_OPTION &&
                       *cp == '-')
                           cp++;
                   for (last = '\n'; *cp != '\0'; ) {
                           if (last == '\n') {
                                 /* Consume all whitespace. */                                  /* Consume all whitespace. */
                                 if (isspace((int)*cp)) {                                  if (isspace((unsigned char)*cp)) {
                                         while (isspace((int)*cp))                                          while (isspace((unsigned char)*cp))
                                                 cp++;                                                  cp++;
                                         continue;                                          continue;
                                 } else if ('\'' == *cp || '.' == *cp)                                  } else if (*cp == '\'' || *cp == '.')
                                         fputs("\\&", stdout);                                          fputs("\\&", stdout);
                         }                          }
                         putchar(last = *cp++);                          putchar(last = *cp++);
                         /* If we're a character escape, escape us. */                          /* If we're a character escape, escape us. */
                         if ('\\' == last)                          if (last == '\\')
                                 putchar('e');                                  putchar('e');
                 }                  }
                 p->newln = 0;  
                 break;                  break;
           case NODE_TITLE:
                   if (pn->parent->node == NODE_BOOKINFO)
                           macro_open(p, "Nd");
                   break;
           case NODE_TYPE:
                   macro_open(p, "Vt");
                   break;
           case NODE_USERINPUT:
                   macro_open(p, "Li");
                   break;
           case NODE_VARIABLELIST:
                   pnode_printvariablelist(p, pn);
                   break;
           case NODE_VARLISTENTRY:
                   pnode_printvarlistentry(p, pn);
                   break;
           case NODE_VARNAME:
                   macro_open(p, "Va");
                   break;
         default:          default:
                 break;                  break;
         }          }
Line 1268  pnode_print(struct parse *p, struct pnode *pn)
Line 857  pnode_print(struct parse *p, struct pnode *pn)
                 pnode_print(p, pp);                  pnode_print(p, pp);
   
         switch (pn->node) {          switch (pn->node) {
         case (NODE_ARG):          case NODE_INFORMALEQUATION:
         case (NODE_CODE):                  macro_line(p, "EN");
         case (NODE_COMMAND):  
         case (NODE_FUNCTION):  
         case (NODE_FUNCSYNOPSISINFO):  
         case (NODE_OPTION):  
         case (NODE_PARAMETER):  
         case (NODE_STRUCTNAME):  
         case (NODE_TEXT):  
                 pnode_printmclose(p, sv);  
                 break;                  break;
         case (NODE_REFNAME):          case NODE_INLINEEQUATION:
                   fputs("$ ", stdout);
                   p->linestate = sv;
                   break;
           case NODE_MML_MROW:
           case NODE_MML_MI:
           case NODE_MML_MN:
           case NODE_MML_MO:
                   if (TAILQ_EMPTY(&pn->childq))
                           break;
                   fputs(" } ", stdout);
                   break;
           case NODE_APPLICATION:
           case NODE_ARG:
           case NODE_AUTHOR:
           case NODE_CITEREFENTRY:
           case NODE_CITETITLE:
           case NODE_CODE:
           case NODE_COMMAND:
           case NODE_CONSTANT:
           case NODE_EDITOR:
           case NODE_EMAIL:
           case NODE_EMPHASIS:
           case NODE_ENVAR:
           case NODE_FILENAME:
           case NODE_FIRSTTERM:
           case NODE_FUNCTION:
           case NODE_FUNCSYNOPSISINFO:
           case NODE_KEYSYM:
           case NODE_LITERAL:
           case NODE_OPTION:
           case NODE_PARAMETER:
           case NODE_REPLACEABLE:
           case NODE_REFPURPOSE:
           case NODE_SGMLTAG:
           case NODE_STRUCTNAME:
           case NODE_TYPE:
           case NODE_USERINPUT:
           case NODE_VARNAME:
                   if (sv != LINE_MACRO && p->linestate == LINE_MACRO)
                           macro_closepunct(p, pn);
                   break;
           case NODE_QUOTE:
                   if (sv == LINE_NEW)
                           macro_close(p);
                   sv = p->linestate;
                   macro_open(p, "Qc");
                   if (sv == LINE_NEW)
                           macro_close(p);
                   break;
           case NODE_REFNAME:
                 /*                  /*
                  * If we're in the NAME macro and we have multiple                   * If we're in the NAME macro and we have multiple
                  * <refname> macros in sequence, then print out a                   * <refname> macros in sequence, then print out a
                  * trailing comma before the newline.                   * trailing comma before the newline.
                  */                   */
                 if (NULL != pn->parent &&                  if (pn->parent != NULL &&
                         NODE_REFNAMEDIV == pn->parent->node &&                      pn->parent->node == NODE_REFNAMEDIV &&
                         NULL != TAILQ_NEXT(pn, child) &&                      TAILQ_NEXT(pn, child) != NULL &&
                         NODE_REFNAME == TAILQ_NEXT(pn, child)->node)                      TAILQ_NEXT(pn, child)->node == NODE_REFNAME)
                         fputs(" ,", stdout);                          macro_addarg(p, ",", ARG_SPACE);
                 pnode_printmclose(p, sv);                  if (sv == LINE_NEW)
                           macro_close(p);
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_PREFACE:
                 assert(p->newln);          case NODE_SECTION:
                 puts(".Ed");          case NODE_NOTE:
                 p->newln = 1;          case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                   p->level--;
                 break;                  break;
           case NODE_LITERALLAYOUT:
           case NODE_PROGRAMLISTING:
           case NODE_SCREEN:
                   macro_line(p, "Ed");
                   break;
           case NODE_TITLE:
                   if (pn->parent->node == NODE_BOOKINFO)
                           macro_line(p, "Sh AUTHORS");
                   break;
         default:          default:
                 break;                  break;
         }          }
 }  }
   
 /*  void
  * Loop around the read buffer until we've drained it of all data.  ptree_print(struct ptree *tree)
  * Invoke the parser context with each buffer fill.  
  */  
 static int  
 readfile(XML_Parser xp, int fd,  
         char *b, size_t bsz, const char *fn)  
 {  {
         struct parse     p;          struct format    formatter;
         int              rc;  
         ssize_t          ssz;  
   
         memset(&p, 0, sizeof(struct parse));          formatter.level = 0;
           formatter.linestate = LINE_NEW;
         p.b = malloc(p.bsz = p.mbsz = 1024);          pnode_printprologue(&formatter, tree);
         p.fname = fn;          pnode_print(&formatter, tree->root);
         p.xml = xp;          if (formatter.linestate != LINE_NEW)
                   putchar('\n');
         XML_SetCharacterDataHandler(xp, xml_char);  
         XML_SetElementHandler(xp, xml_elem_start, xml_elem_end);  
         XML_SetUserData(xp, &p);  
   
         while ((ssz = read(fd, b, bsz)) >= 0) {  
                 if (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.12  
changed lines
  Added in v.1.80

CVSweb