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

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.8 and 1.109

version 1.8, 2014/03/28 12:11:18 version 1.109, 2019/04/12 10:34:48
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 */  
 };  
   
 struct  node {  static void      pnode_print(struct format *, struct pnode *);
         const char      *name; /* docbook element name */  
         unsigned int     flags;  
 #define NODE_IGNTEXT     1 /* ignore all contained text */  
 };  
   
 TAILQ_HEAD(pnodeq, pnode);  
   
 struct  pnode {  static void
         enum nodeid      node; /* node type */  pnode_printtext(struct format *f, struct pnode *n)
         char            *b; /* binary data buffer */  
         size_t           bsz; /* data buffer size */  
         struct pnode    *parent; /* parent (or NULL if top) */  
         struct pnodeq    childq; /* queue of children */  
         TAILQ_ENTRY(pnode) child;  
 };  
   
 static  const struct node nodes[NODE__MAX] = {  
         { NULL, 0 },  
         { "arg", 0 },  
         { "citerefentry", NODE_IGNTEXT },  
         { "cmdsynopsis", NODE_IGNTEXT },  
         { "code", 0 },  
         { "command", 0 },  
         { "funcdef", 0 },  
         { "funcprototype", NODE_IGNTEXT },  
         { "funcsynopsis", NODE_IGNTEXT },  
         { "funcsynopsisinfo", 0 },  
         { "function", 0 },  
         { "manvolnum", 0 },  
         { "option", 0 },  
         { "para", 0 },  
         { "paramdef", 0 },  
         { "parameter", 0 },  
         { "programlisting", 0 },  
         { "refclass", NODE_IGNTEXT },  
         { "refdescriptor", NODE_IGNTEXT },  
         { "refentry", NODE_IGNTEXT },  
         { "refentrytitle", 0 },  
         { "refmeta", NODE_IGNTEXT },  
         { "refmiscinfo", NODE_IGNTEXT },  
         { "refname", 0 },  
         { "refnamediv", NODE_IGNTEXT },  
         { "refpurpose", 0 },  
         { "refsect1", 0 },  
         { "refsynopsisdiv", NODE_IGNTEXT },  
         { "structname", 0 },  
         { "synopsis", 0 },  
         { NULL, 0 },  
         { "title", 0 },  
 };  
   
 /*  
  * Look up whether "parent" is a valid parent for "node".  
  * This is sucked directly from the DocBook specification: look at the  
  * "children" and "parent" sections of each node.  
  */  
 static int  
 isparent(enum nodeid node, enum nodeid parent)  
 {  {
           struct pnode    *nn;
           char            *cp;
           int              accept_arg;
   
         switch (node) {          cp = n->b;
         case (NODE_ROOT):          accept_arg = f->flags & FMT_ARG;
                 return(0);          if (f->linestate == LINE_MACRO && !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):  
                 switch (parent) {  
                 case (NODE_PARA):  
                 case (NODE_REFSECT1):  
                 case (NODE_REFSYNOPSISDIV):  
                         return(1);  
                 default:  
                         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:  
                         break;  
                 }  
                 return(0);  
         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;  
                 }  
                 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;  
         }          }
           if (f->linestate == LINE_MACRO && !accept_arg &&
               (f->flags & (FMT_CHILD | FMT_IMPL)) == 0)
                   macro_close(f);
   
         abort();          /*
         return(0);           * Text preceding a macro without intervening whitespace
 }           * requires a .Pf macro.
            * Set the spacing flag to avoid a redundant .Ns macro.
            */
   
 /*          if (f->linestate != LINE_MACRO &&
  * Process a stream of characters.              (nn = TAILQ_NEXT(n, child)) != NULL && nn->spc == 0 &&
  * We store text as nodes in and of themselves.              (nn->node != NODE_TEXT && nn->node != NODE_ESCAPE)) {
  * If a text node is already open, append to it.                  macro_open(f, "Pf");
  * If it's not open, open one under the current context.                  accept_arg = 1;
  */                  f->flags |= FMT_CHILD;
 static void                  nn->spc = 1;
 xml_char(void *arg, const XML_Char *p, int sz)          }
 {  
         struct parse    *ps = arg;  
         struct pnode    *dat;  
         int              i;  
   
         /* Stopped or no tree yet. */          switch (f->linestate) {
         if (ps->stop || NODE_ROOT == ps->node)          case LINE_NEW:
                 return;                  break;
           case LINE_TEXT:
                   if (n->spc) {
                           if (n->node == NODE_TEXT)
                                   macro_close(f);
                           else
                                   putchar(' ');
                   }
                   break;
           case LINE_MACRO:
                   if (accept_arg)
                           putchar(' ');
                   else
                           macro_close(f);
                   break;
           }
   
         /* Not supposed to be collecting text. */          if (n->node == NODE_ESCAPE) {
         assert(NULL != ps->cur);                  fputs(n->b, stdout);
         if (NODE_IGNTEXT & nodes[ps->node].flags)                  if (f->linestate == LINE_NEW)
                           f->linestate = LINE_TEXT;
                 return;                  return;
           }
   
         /*          /*
          * Are we in the midst of processing text?           * Remove the prefix '-' from <option> elements
          * If we're not processing text right now, then create a text           * because the arguments of .Fl macros do not need it.
          * node for doing so.  
          * However, don't do so unless we have some non-whitespace to  
          * process!  
          */           */
         if (NODE_TEXT != ps->node) {  
                 for (i = 0; i < sz; i++)  
                         if ( ! isspace((int)p[i]))  
                                 break;  
                 if (i == sz)  
                         return;  
                 dat = calloc(1, sizeof(struct pnode));  
                 if (NULL == dat) {  
                         perror(NULL);  
                         exit(EXIT_FAILURE);  
                 }  
   
                 dat->node = ps->node = NODE_TEXT;          if (n->parent != NULL && n->parent->node == NODE_OPTION && *cp == '-')
                 dat->parent = ps->cur;                  cp++;
                 TAILQ_INIT(&dat->childq);  
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);  
                 ps->cur = dat;  
                 assert(NULL != ps->root);  
   
         }          if (f->linestate == LINE_MACRO)
                   macro_addarg(f, cp, 0);
         /* Append to current buffer. */          else
         assert(sz >= 0);                  print_text(f, cp, 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;  
 }  }
   
 /*  
  * 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  static void
 xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts)  pnode_printpara(struct format *f, struct pnode *n)
 {  {
         struct parse    *ps = arg;          struct pnode    *np;
         enum nodeid      node;  
         struct pnode    *dat;  
   
         if (ps->stop)          if (n->parent == NULL)
                 return;                  return;
   
         /* Close out text node, if applicable... */          if ((np = TAILQ_PREV(n, pnodeq, child)) == NULL)
         if (NODE_TEXT == ps->node) {              np = n->parent;
                 assert(NULL != ps->cur);  
                 ps->cur = ps->cur->parent;  
                 assert(NULL != ps->cur);  
                 ps->node = ps->cur->node;  
         }  
   
         for (node = 0; node < NODE__MAX; node++)          f->flags = 0;
                 if (NULL == nodes[node].name)  
                         continue;  
                 else if (0 == strcmp(nodes[node].name, name))  
                         break;  
   
         /* FIXME: do more with these error messages... */          switch (np->node) {
         if (NODE__MAX == node && NODE_ROOT == ps->node) {          case NODE_ENTRY:
                 fprintf(stderr, "%s: ignoring node\n", name);          case NODE_GLOSSTERM:
           case NODE_LISTITEM:
           case NODE_TERM:
                 return;                  return;
         } else if (NODE__MAX == node) {          case NODE_APPENDIX:
                 fprintf(stderr, "%s: unknown node\n", name);          case NODE_LEGALNOTICE:
                 ps->stop = 1;          case NODE_PREFACE:
                 return;          case NODE_SECTION:
         } else if (NODE_ROOT == ps->node && NULL != ps->root) {                  if (f->level < 3)
                 fprintf(stderr, "%s: reentering?\n", name);                          return;
                 ps->stop = 1;                  break;
                 return;          default:
         } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {                  break;
                 fprintf(stderr, "%s: known node w/o context\n", name);  
                 return;  
         } else if ( ! isparent(node, ps->node)) {  
                 fprintf(stderr, "%s: bad parent\n", name);  
                 ps->stop = 1;  
                 return;  
         }          }
           macro_line(f, "Pp");
         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);  
   
         if (NULL != ps->cur)  
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);  
   
         ps->cur = dat;  
         if (NULL == ps->root)  
                 ps->root = dat;  
 }  }
   
 /*  /*
  * Roll up the parse tree.   * If the SYNOPSIS macro has a superfluous title, kill it.
  * 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_printrefsynopsisdiv(struct format *f, struct pnode *n)
 {  {
         struct parse    *ps = arg;          struct pnode    *nc, *nn;
   
         if (ps->stop || NODE_ROOT == ps->node)          TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn)
                 return;                  if (nc->node == NODE_TITLE)
                           pnode_unlink(nc);
   
         /* Close out text node, if applicable... */          macro_line(f, "Sh SYNOPSIS");
         if (NODE_TEXT == ps->node) {  
                 assert(NULL != ps->cur);  
                 ps->cur = ps->cur->parent;  
                 assert(NULL != ps->cur);  
                 ps->node = ps->cur->node;  
         }  
   
         if (NULL == (ps->cur = ps->cur->parent))  
                 ps->node = NODE_ROOT;  
         else  
                 ps->node = ps->cur->node;  
 }  }
   
 /*  /*
  * Recursively free a node (NULL is ok).   * Start a hopefully-named `Sh' section.
  */   */
 static void  static void
 pnode_free(struct pnode *pn)  pnode_printrefsect(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc;
           const char      *title;
           int              flags, level;
   
         if (NULL == pn)          if (n->parent == NULL)
                 return;                  return;
   
         while (NULL != (pp = TAILQ_FIRST(&pn->childq))) {          level = ++f->level;
                 TAILQ_REMOVE(&pn->childq, pp, child);          flags = ARG_SPACE;
                 pnode_free(pp);          if (level == 1)
                   flags |= ARG_UPPER;
           if (level < 3) {
                   switch (n->node) {
                   case NODE_CAUTION:
                   case NODE_NOTE:
                   case NODE_TIP:
                   case NODE_WARNING:
                           level = 3;
                           break;
                   default:
                           break;
                   }
         }          }
   
         free(pn->b);          TAILQ_FOREACH(nc, &n->childq, child)
         free(pn);                  if (nc->node == NODE_TITLE)
                           break;
   
           if (nc == NULL) {
                   switch (n->node) {
                   case NODE_PREFACE:
                           title = "Preface";
                           break;
                   case NODE_APPENDIX:
                           title = "Appendix";
                           break;
                   case NODE_LEGALNOTICE:
                           title = "Legal Notice";
                           break;
                   case NODE_CAUTION:
                           title = "Caution";
                           break;
                   case NODE_NOTE:
                           title = "Note";
                           break;
                   case NODE_TIP:
                           title = "Tip";
                           break;
                   case NODE_WARNING:
                           title = "Warning";
                           break;
                   default:
                           title = "Unknown";
                           break;
                   }
           }
   
           switch (level) {
           case 1:
                   macro_close(f);
                   macro_open(f, "Sh");
                   break;
           case 2:
                   macro_close(f);
                   macro_open(f, "Ss");
                   break;
           default:
                   pnode_printpara(f, n);
                   macro_open(f, "Sy");
                   break;
           }
   
           if (nc != NULL) {
                   macro_addnode(f, nc, flags);
                   pnode_unlink(nc);
           } else
                   macro_addarg(f, title, flags | ARG_QUOTED);
           macro_close(f);
 }  }
   
 /*  /*
  * Unlink a node from its parent and pnode_free() it.   * Start a reference, extracting the title and volume.
  */   */
 static void  static void
 pnode_unlink(struct pnode *pn)  pnode_printciterefentry(struct format *f, struct pnode *n)
 {  {
           struct pnode    *nc, *title, *manvol;
   
         if (NULL != pn->parent)          title = manvol = NULL;
                 TAILQ_REMOVE(&pn->parent->childq, pn, child);          TAILQ_FOREACH(nc, &n->childq, child) {
         pnode_free(pn);                  if (nc->node == NODE_MANVOLNUM)
                           manvol = nc;
                   else if (nc->node == NODE_REFENTRYTITLE)
                           title = nc;
           }
           macro_open(f, "Xr");
           if (title == NULL)
                   macro_addarg(f, "unknown", ARG_SPACE);
           else
                   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);
 }  }
   
 /*  
  * Unlink all children of a node and pnode_free() them.  
  */  
 static void  static void
 pnode_unlinksub(struct pnode *pn)  pnode_printrefmeta(struct format *f, struct pnode *n)
 {  {
           struct pnode    *nc, *title, *manvol;
   
         while ( ! TAILQ_EMPTY(&pn->childq))          title = manvol = NULL;
                 pnode_unlink(TAILQ_FIRST(&pn->childq));          TAILQ_FOREACH(nc, &n->childq, child) {
                   if (nc->node == NODE_MANVOLNUM)
                           manvol = nc;
                   else if (nc->node == NODE_REFENTRYTITLE)
                           title = nc;
           }
           macro_close(f);
           macro_open(f, "Dt");
           if (title == NULL)
                   macro_addarg(f, "UNKNOWN", ARG_SPACE);
           else
                   macro_addnode(f, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER);
           if (manvol == NULL)
                   macro_addarg(f, "1", ARG_SPACE);
           else
                   macro_addnode(f, manvol, ARG_SPACE | ARG_SINGLE);
           macro_close(f);
           pnode_unlink(n);
 }  }
   
 /*  
  * Reset the lookaside buffer.  
  */  
 static void  static void
 bufclear(struct parse *p)  pnode_printfuncdef(struct format *f, struct pnode *n)
 {  {
           struct pnode    *nc;
   
         p->b[p->bsz = 0] = '\0';          nc = TAILQ_FIRST(&n->childq);
           if (nc != NULL && nc->node == NODE_TEXT) {
                   macro_argline(f, "Ft", nc->b);
                   pnode_unlink(nc);
           }
           macro_nodeline(f, "Fo", n, ARG_SINGLE);
           pnode_unlinksub(n);
 }  }
   
 /*  /*
  * Append NODE_TEXT contents to the current buffer, reallocating its   * The <mml:mfenced> node is a little peculiar.
  * size if necessary.   * First, it can have arbitrary open and closing tokens, which default
  * The buffer is ALWAYS nil-terminated.   * to parentheses.
    * Second, >1 arguments are separated by commas.
  */   */
 static void  static void
 bufappend(struct parse *p, struct pnode *pn)  pnode_printmathfenced(struct format *f, struct pnode *n)
 {  {
           struct pnode    *nc;
   
         assert(NODE_TEXT == pn->node);          printf("left %s ", pnode_getattr_raw(n, ATTRKEY_OPEN, "("));
         if (p->bsz + pn->bsz + 1 > p->mbsz) {  
                 p->mbsz = p->bsz + pn->bsz + 1;          nc = TAILQ_FIRST(&n->childq);
                 if (NULL == (p->b = realloc(p->b, p->mbsz))) {          pnode_print(f, nc);
                         perror(NULL);  
                         exit(EXIT_FAILURE);          while ((nc = TAILQ_NEXT(nc, child)) != NULL) {
                 }                  putchar(',');
                   pnode_print(f, nc);
         }          }
         memcpy(p->b + p->bsz, pn->b, pn->bsz);          printf("right %s ", pnode_getattr_raw(n, ATTRKEY_CLOSE, ")"));
         p->bsz += pn->bsz;          pnode_unlinksub(n);
         p->b[p->bsz] = '\0';  
 }  }
   
 /*  /*
  * Recursively append all NODE_TEXT nodes to the buffer.   * These math nodes require special handling because they have infix
  * This descends into non-text nodes, but doesn't do anything beyond   * syntax, instead of the usual prefix or prefix.
  * them.   * So we need to break up the first and second child node with a
  * In other words, this is a recursive text grok.   * particular eqn(7) word.
  */   */
 static void  static void
 bufappend_r(struct parse *p, struct pnode *pn)  pnode_printmath(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc;
   
         if (NODE_TEXT == pn->node)          nc = TAILQ_FIRST(&n->childq);
                 bufappend(p, pn);          pnode_print(f, nc);
         TAILQ_FOREACH(pp, &pn->childq, child)  
                 bufappend_r(p, pp);          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);
 }  }
   
 /*  
  * 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_printfuncprototype(struct format *f, struct pnode *n)
 {  {
         char            *cp;          struct pnode    *nc, *fdef;
   
         bufclear(p);          TAILQ_FOREACH(fdef, &n->childq, child)
         bufappend_r(p, pn);                  if (fdef->node == NODE_FUNCDEF)
                           break;
   
         /* Convert all space to spaces. */          if (fdef != NULL) {
         for (cp = p->b; '\0' != *cp; cp++)                  pnode_printfuncdef(f, fdef);
                 if (isspace((int)*cp))                  pnode_unlink(fdef);
                         *cp = ' ';          } else
                   macro_line(f, "Fo UNKNOWN");
   
         for (cp = p->b; isspace((int)*cp); cp++)          TAILQ_FOREACH(nc, &n->childq, child)
                 /* Spin past whitespace (XXX: necessary?) */ ;                  macro_nodeline(f, "Fa", nc, ARG_SINGLE);
         for ( ; '\0' != *cp; cp++) {  
                 /* Escape us if we look like a macro. */          macro_line(f, "Fc");
                 if ((cp == p->b || ' ' == *(cp - 1)) &&          pnode_unlinksub(n);
                         isupper((int)*cp) &&  
                         '\0' != *(cp + 1) &&  
                         islower((int)*(cp + 1)) &&  
                         ('\0' == *(cp + 2) ||  
                          ' ' == *(cp + 2) ||  
                          (islower((int)*(cp + 2)) &&  
                           ('\0' == *(cp + 3) ||  
                            ' ' == *(cp + 3)))))  
                         fputs("\\&", stdout);  
                 putchar(*cp);  
                 /* If we're a character escape, escape us. */  
                 if ('\\' == *cp)  
                         putchar('e');  
         }  
 }  }
   
 /*  /*
  * Just pnode_printmacrolinepart() but with a newline.   * The <arg> element is more complicated than it should be because text
  * If no text, just the newline.   * 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_printmacroline(struct parse *p, struct pnode *pn)  pnode_printarg(struct format *f, struct pnode *n)
 {  {
           struct pnode    *nc;
           struct pattr    *a;
           int              isop, isrep, was_impl;
   
         pnode_printmacrolinepart(p, pn);          isop = 1;
         putchar('\n');          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 && nc->node == NODE_TEXT)
                           macro_addarg(f, "...", ARG_SPACE);
           }
           if (isop) {
                   if (was_impl)
                           macro_open(f, "Oc");
                   else
                           f->flags &= ~FMT_IMPL;
           }
           pnode_unlinksub(n);
 }  }
   
 /*  
  * Start the SYNOPSIS macro, unlinking its [superfluous] title.  
  */  
 static void  static void
 pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn)  pnode_printgroup(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc, *nn;
           struct pattr    *a;
           int              isop, sv;
   
         TAILQ_FOREACH(pp, &pn->childq, child)          isop = 1;
                 if (NODE_TITLE == pp->node) {          TAILQ_FOREACH(a, &n->attrq, child)
                         pnode_unlink(pp);                  if (a->key == ATTRKEY_CHOICE &&
                       (a->val == ATTRVAL_PLAIN || a->val == ATTRVAL_REQ)) {
                           isop = 0;
                         break;                          break;
                 }                  }
   
         puts(".Sh SYNOPSIS");          /*
            * Make sure we're on a macro line.
            * This will prevent pnode_print() for putting us on a
            * subsequent line.
            */
           sv = f->linestate == LINE_NEW;
           if (isop)
                   macro_open(f, "Op");
           else if (sv)
                   macro_open(f, "No");
           f->flags |= FMT_IMPL;
   
           /*
            * Keep on printing text separated by the vertical bar as long
            * as we're within the same origin node as the group.
            * This is kind of a nightmare.
            * Eh, DocBook...
            * FIXME: if there's a "Fl", we don't cut off the leading "-"
            * like we do in pnode_print().
            */
           TAILQ_FOREACH(nc, &n->childq, child) {
                   pnode_print(f, nc);
                   nn = TAILQ_NEXT(nc, child);
                   while (nn != NULL) {
                           if (nc->node != nn->node)
                                   break;
                           macro_addarg(f, "|", ARG_SPACE);
                           macro_addnode(f, nn, ARG_SPACE);
                           nc = nn;
                           nn = TAILQ_NEXT(nn, child);
                   }
           }
           if (sv)
                   macro_close(f);
           f->flags &= ~FMT_IMPL;
           pnode_unlinksub(n);
 }  }
   
 /*  
  * Start a hopefully-named `Sh' section.  
  */  
 static void  static void
 pnode_printrefsect(struct parse *p, struct pnode *pn)  pnode_printauthor(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc, *nn;
           int              have_contrib, have_name;
   
         TAILQ_FOREACH(pp, &pn->childq, child)          /*
                 if (NODE_TITLE == pp->node)           * Print <contrib> children up front, before the .An scope,
            * and figure out whether we a name of a person.
            */
   
           have_contrib = have_name = 0;
           TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
                   switch (nc->node) {
                   case NODE_CONTRIB:
                           if (have_contrib)
                                   print_text(f, ",", 0);
                           print_textnode(f, nc);
                           pnode_unlink(nc);
                           have_contrib = 1;
                         break;                          break;
                   case NODE_PERSONNAME:
                           have_name = 1;
                           break;
                   default:
                           break;
                   }
           }
           if (TAILQ_FIRST(&n->childq) == NULL)
                   return;
   
         fputs(".Sh ", stdout);          if (have_contrib)
                   print_text(f, ":", 0);
   
         if (NULL != pp) {          /*
                 pnode_printmacroline(p, pp);           * 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
         } else           * scope.  Otherwise, print everything in the scope.
                 puts("UNKNOWN");           */
 }  
   
 /*          macro_open(f, "An");
  * Start a reference, extracting the title and volume.          TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
  */                  if (nc->node == NODE_PERSONNAME || have_name == 0) {
 static void                          macro_addnode(f, nc, ARG_SPACE);
 pnode_printciterefentry(struct parse *p, struct pnode *pn)                          pnode_unlink(nc);
 {                  }
         struct pnode    *pp, *title, *manvol;          }
   
         title = manvol = NULL;          /*
         TAILQ_FOREACH(pp, &pn->childq, child)           * If there is an email address,
                 if (NODE_MANVOLNUM == pp->node)           * print it on the same macro line.
                         manvol = pp;           */
                 else if (NODE_REFENTRYTITLE == pp->node)  
                         title = pp;  
   
         fputs(".Xr ", stdout);          if ((nc = pnode_findfirst(n, NODE_EMAIL)) != NULL) {
                   f->flags |= FMT_CHILD;
                   pnode_print(f, nc);
                   pnode_unlink(nc);
           }
   
         if (NULL != title) {          /*
                 pnode_printmacrolinepart(p, title);           * If there are still unprinted children, end the scope
                 putchar(' ');           * with a comma.  Otherwise, leave the scope open in case
         } else           * a text node follows that starts with closing punctuation.
                 fputs("unknown ", stdout);           */
   
         if (NULL != manvol)          if (TAILQ_FIRST(&n->childq) != NULL) {
                 pnode_printmacroline(p, manvol);                  macro_addarg(f, ",", ARG_SPACE);
         else                  macro_close(f);
                 puts("1");          }
 }  }
   
 static void  static void
 pnode_printrefmeta(struct parse *p, struct pnode *pn)  pnode_printlink(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp, *title, *manvol;          struct pnode    *nc;
           const char      *uri, *text;
   
         title = manvol = NULL;          uri = pnode_getattr_raw(n, ATTRKEY_LINKEND, NULL);
         TAILQ_FOREACH(pp, &pn->childq, child)          if (uri != NULL) {
                 if (NODE_MANVOLNUM == pp->node)                  if (TAILQ_FIRST(&n->childq) != NULL) {
                         manvol = pp;                          TAILQ_FOREACH(nc, &n->childq, child)
                 else if (NODE_REFENTRYTITLE == pp->node)                                  pnode_print(f, nc);
                         title = pp;                          text = "";
                   } else if ((text = pnode_getattr_raw(n,
                       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);
                   return;
           }
   }
   
         puts(".Dd $Mdocdate" "$");  static void
         fputs(".Dt ", stdout);  pnode_printprologue(struct format *f, struct ptree *tree)
   {
           struct pnode    *refmeta;
   
         if (NULL != title) {          refmeta = tree->root == NULL ? NULL :
                 /* FIXME: uppercase. */              pnode_findfirst(tree->root, NODE_REFMETA);
                 pnode_printmacrolinepart(p, title);  
                 putchar(' ');          macro_line(f, "Dd $Mdocdate" "$");
           if (refmeta == NULL) {
                   macro_open(f, "Dt");
                   macro_addarg(f,
                       pnode_getattr_raw(tree->root, ATTRKEY_ID, "UNKNOWN"),
                       ARG_SPACE | ARG_SINGLE | ARG_UPPER);
                   macro_addarg(f, "1", ARG_SPACE);
                   macro_close(f);
         } else          } else
                 fputs("UNKNOWN ", stdout);                  pnode_printrefmeta(f, refmeta);
           macro_line(f, "Os");
   
         if (NULL != manvol)          if (tree->flags & TREE_EQN) {
                 pnode_printmacroline(p, manvol);                  macro_line(f, "EQ");
         else                  print_text(f, "delim $$", 0);
                 puts("1");                  macro_line(f, "EN");
           }
         puts(".Os");  
 }  }
   
   /*
    * We can have multiple <term> elements within a <varlistentry>, which
    * we should comma-separate as list headers.
    */
 static void  static void
 pnode_printfuncdef(struct parse *p, struct pnode *pn)  pnode_printvarlistentry(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp, *ftype, *func;          struct pnode    *nc, *nn;
           int              first = 1;
   
         ftype = func = NULL;          macro_open(f, "It");
         TAILQ_FOREACH(pp, &pn->childq, child)          f->flags |= FMT_IMPL;
                 if (NODE_TEXT == pp->node)          TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
                         ftype = pp;                  if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM)
                 else if (NODE_FUNCTION == pp->node)                          continue;
                         func = pp;                  if (first == 0) {
                           switch (f->linestate) {
         if (NULL != ftype) {                          case LINE_NEW:
                 fputs(".Ft ", stdout);                                  break;
                 pnode_printmacroline(p, ftype);                          case LINE_TEXT:
                                   print_text(f, ",", 0);
                                   break;
                           case LINE_MACRO:
                                   macro_addarg(f, ",", 0);
                                   break;
                           }
                   }
                   pnode_print(f, nc);
                   pnode_unlink(nc);
                   first = 0;
         }          }
           macro_close(f);
         if (NULL != func) {          while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
                 fputs(".Fo ", stdout);                  pnode_print(f, nc);
                 pnode_printmacroline(p, func);                  pnode_unlink(nc);
         } else          }
                 puts(".Fo UNKNOWN");          macro_close(f);
 }  }
   
 static void  static void
 pnode_printparamdef(struct parse *p, struct pnode *pn)  pnode_printtitle(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp, *ptype, *param;          struct pnode    *nc, *nn;
   
         ptype = param = NULL;          TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
         TAILQ_FOREACH(pp, &pn->childq, child)                  if (nc->node == NODE_TITLE) {
                 if (NODE_TEXT == pp->node)                          pnode_printpara(f, nc);
                         ptype = pp;                          pnode_print(f, nc);
                 else if (NODE_PARAMETER == pp->node)                          pnode_unlink(nc);
                         param = pp;                  }
   
         fputs(".Fa \"", stdout);  
         if (NULL != ptype) {  
                 pnode_printmacrolinepart(p, ptype);  
                 putchar(' ');  
         }          }
   }
   
         if (NULL != param)  static void
                 pnode_printmacrolinepart(p, param);  pnode_printrow(struct format *f, struct pnode *n)
         else  {
                 fputs("UNKNOWN", stdout);          struct pnode    *nc;
   
         puts("\"");          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);
 }  }
   
 static void  static void
 pnode_printfuncprototype(struct parse *p, struct pnode *pn)  pnode_printtgroup1(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp, *fdef;          struct pnode    *nc;
   
         TAILQ_FOREACH(fdef, &pn->childq, child)          macro_line(f, "Bl -bullet -compact");
                 if (NODE_FUNCDEF == fdef->node)          while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) {
                         break;                  macro_line(f, "It");
                   pnode_print(f, nc);
                   pnode_unlink(nc);
           }
           macro_line(f, "El");
           pnode_unlinksub(n);
   }
   
         if (NULL != fdef)  static void
                 pnode_printfuncdef(p, fdef);  pnode_printtgroup2(struct format *f, struct pnode *n)
         else  {
                 puts(".Fo UNKNOWN");          struct pnode    *nr, *ne;
   
         TAILQ_FOREACH(pp, &pn->childq, child)          macro_line(f, "Bl -tag -width Ds");
                 if (NODE_PARAMDEF == pp->node)          while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) {
                         pnode_printparamdef(p, pp);                  if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL)
                           break;
         puts(".Fc");                  macro_open(f, "It");
                   f->flags |= FMT_IMPL;
                   pnode_print(f, ne);
                   macro_close(f);
                   pnode_unlink(ne);
                   pnode_print(f, nr);
                   pnode_unlink(nr);
           }
           macro_line(f, "El");
           pnode_unlinksub(n);
 }  }
   
 /* TODO: handle "optional" values. */  
 static void  static void
 pnode_printarg(struct parse *p, struct pnode *pn, int nested)  pnode_printtgroup(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc;
         int              sv = nested;  
   
         if ( ! nested)          switch (atoi(pnode_getattr_raw(n, ATTRKEY_COLS, "0"))) {
                 fputs(".", stdout);          case 1:
         nested = 1;                  pnode_printtgroup1(f, n);
         TAILQ_FOREACH(pp, &pn->childq, child)                  return;
                 if (NODE_OPTION == pp->node) {          case 2:
                         fputs("Fl ", stdout);                  pnode_printtgroup2(f, n);
                         pnode_printmacrolinepart(p, pp);                  return;
                 } else if (NODE_TEXT == pp->node) {          default:
                         fputs("Ar ", stdout);                  break;
                         pnode_printmacrolinepart(p, pp);          }
                 } else if (NODE_ARG == pp->node)  
                         pnode_printarg(p, pp, nested);  
   
         if ( ! sv)          macro_line(f, "Bl -ohang");
                 puts("");          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
  * Recursively search and return the first instance of "node".  pnode_printlist(struct format *f, struct pnode *n)
  */  
 static struct pnode *  
 pnode_findfirst(struct pnode *pn, enum nodeid node)  
 {  {
         struct pnode    *pp, *res;          struct pnode    *nc;
   
         res = NULL;          pnode_printtitle(f, n);
         TAILQ_FOREACH(pp, &pn->childq, child) {          macro_argline(f, "Bl",
                 res = pp->node == node ? pp :              n->node == NODE_ORDEREDLIST ? "-enum" : "-bullet");
                         pnode_findfirst(pp, node);          TAILQ_FOREACH(nc, &n->childq, child) {
                 if (NULL != res)                  macro_line(f, "It");
                         break;                  pnode_print(f, nc);
         }          }
           macro_line(f, "El");
         return(res);          pnode_unlinksub(n);
 }  }
   
 static void  static void
 pnode_printprologue(struct parse *p, struct pnode *pn)  pnode_printvariablelist(struct format *f, struct pnode *n)
 {  {
         struct pnode    *pp;          struct pnode    *nc;
   
         if (NULL != (pp = pnode_findfirst(p->root, NODE_REFMETA))) {          pnode_printtitle(f, n);
                 pnode_printrefmeta(p, pp);          macro_line(f, "Bl -tag -width Ds");
                 pnode_unlink(pp);          TAILQ_FOREACH(nc, &n->childq, child) {
         } else {                  if (nc->node == NODE_VARLISTENTRY)
                 puts(".\\\" Supplying bogus prologue...");                          pnode_printvarlistentry(f, nc);
                 puts(".Dd $Mdocdate" "$");                  else
                 puts(".Dt UNKNOWN 1");                          macro_nodeline(f, "It", nc, 0);
                 puts(".Os");  
         }          }
           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;          enum linestate   sv;
         int              last;  
   
         if (NULL == pn)          if (n == NULL)
                 return;                  return;
   
         if (NODE_TEXT != pn->node && NODE_ROOT != pn->node)          sv = f->linestate;
                 printf(".\\\" %s\n", nodes[pn->node].name);          if (n->spc)
                   f->flags &= ~FMT_NOSPC;
           else
                   f->flags |= FMT_NOSPC;
   
         switch (pn->node) {          switch (n->node) {
         case (NODE_ARG):          case NODE_APPLICATION:
                 pnode_printarg(p, pn, 0);                  macro_open(f, "Nm");
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CITEREFENTRY):          case NODE_ARG:
                 pnode_printciterefentry(p, pn);                  pnode_printarg(f, n);
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CODE):          case NODE_AUTHOR:
                 fputs(".Li ", stdout);                  pnode_printauthor(f, n);
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_COMMAND):          case NODE_AUTHORGROUP:
                 fputs(".Nm ", stdout);                  macro_line(f, "An -split");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCTION):          case NODE_BLOCKQUOTE:
                 fputs(".Fn ", stdout);                  macro_line(f, "Bd -ragged -offset indent");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCPROTOTYPE):          case NODE_BOOKINFO:
                 pnode_printfuncprototype(p, pn);                  macro_line(f, "Sh NAME");
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCSYNOPSISINFO):          case NODE_CITEREFENTRY:
                 fputs(".Fd ", stdout);                  pnode_printciterefentry(f, n);
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_PARA):          case NODE_CITETITLE:
                 /* FIXME: not always. */                  macro_open(f, "%T");
                 puts(".Pp");  
                 break;                  break;
         case (NODE_PARAMETER):          case NODE_COMMAND:
                 fputs(".Fa \"", stdout);                  macro_open(f, "Nm");
                 pnode_printmacrolinepart(p, pn);  
                 puts("\"");  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_CONSTANT:
                 puts(".Bd -literal");                  macro_open(f, "Dv");
                 break;                  break;
         case (NODE_REFMETA):          case NODE_EDITOR:
                 abort();                  print_text(f, "editor:", ARG_SPACE);
                   sv = LINE_TEXT;
                   macro_open(f, "An");
                 break;                  break;
         case (NODE_REFNAME):          case NODE_EMAIL:
                 fputs(".Nm ", stdout);                  macro_open(f, "Aq Mt");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 return;  
         case (NODE_REFNAMEDIV):  
                 puts(".Sh NAME");  
                 break;                  break;
         case (NODE_REFPURPOSE):          case NODE_EMPHASIS:
                 fputs(".Nd ", stdout);          case NODE_FIRSTTERM:
                 pnode_printmacroline(p, pn);          case NODE_GLOSSTERM:
                 pnode_unlinksub(pn);                  macro_open(f, "Em");
                 return;  
         case (NODE_REFSYNOPSISDIV):  
                 pnode_printrefsynopsisdiv(p, pn);  
                 break;                  break;
         case (NODE_REFSECT1):          case NODE_ENVAR:
                 pnode_printrefsect(p, pn);                  macro_open(f, "Ev");
                 break;                  break;
         case (NODE_STRUCTNAME):          case NODE_ERRORNAME:
                 fputs(".Vt ", stdout);                  macro_open(f, "Er");
                 pnode_printmacroline(p, pn);                  break;
                 pnode_unlinksub(pn);          case NODE_FILENAME:
                 return;                  macro_open(f, "Pa");
         case (NODE_TEXT):                  break;
                 bufclear(p);          case NODE_FUNCTION:
                 bufappend(p, pn);                  macro_open(f, "Fn");
                 /*                  break;
                  * Output all characters, squeezing out whitespace          case NODE_FUNCPROTOTYPE:
                  * between newlines.                  pnode_printfuncprototype(f, n);
                  * XXX: all whitespace, including tabs (?).                  break;
                  * Remember to escape control characters and escapes.          case NODE_FUNCSYNOPSISINFO:
                  */                  macro_open(f, "Fd");
                 for (last = '\n', cp = p->b; '\0' != *cp; ) {                  break;
                         if ('\n' == last) {          case NODE_INFORMALEQUATION:
                                 /* Consume all whitespace. */                  macro_line(f, "EQ");
                                 if (isspace((int)*cp)) {                  break;
                                         while (isspace((int)*cp))          case NODE_INLINEEQUATION:
                                                 cp++;                  if (f->linestate == LINE_NEW)
                                         continue;                          f->linestate = LINE_TEXT;
                                 } else if ('\'' == *cp || '.' == *cp)                  putchar('$');
                                         fputs("\\&", stdout);                  break;
                         }          case NODE_ITEMIZEDLIST:
                         putchar(last = *cp++);                  pnode_printlist(f, n);
                         /* If we're a character escape, escape us. */                  break;
                         if ('\\' == last)          case NODE_GROUP:
                                 putchar('e');                  pnode_printgroup(f, n);
                   break;
           case NODE_KEYSYM:
                   macro_open(f, "Sy");
                   break;
           case NODE_LINK:
                   pnode_printlink(f, n);
                   break;
           case NODE_LITERAL:
                   macro_open(f, "Ql");
                   break;
           case NODE_LITERALLAYOUT:
                   macro_close(f);
                   macro_argline(f, "Bd", pnode_getattr(n, ATTRKEY_CLASS) ==
                       ATTRVAL_MONOSPACED ? "-literal" : "-unfilled");
                   break;
           case NODE_MARKUP:
                   macro_open(f, "Ic");
                   break;
           case NODE_MML_MFENCED:
                   pnode_printmathfenced(f, n);
                   break;
           case NODE_MML_MROW:
           case NODE_MML_MI:
           case NODE_MML_MN:
           case NODE_MML_MO:
                   if (TAILQ_EMPTY(&n->childq))
                           break;
                   fputs(" { ", stdout);
                   break;
           case NODE_MML_MFRAC:
           case NODE_MML_MSUB:
           case NODE_MML_MSUP:
                   pnode_printmath(f, n);
                   break;
           case NODE_OPTION:
                   macro_open(f, "Fl");
                   break;
           case NODE_ORDEREDLIST:
                   pnode_printlist(f, n);
                   break;
           case NODE_PARA:
                   pnode_printpara(f, n);
                   break;
           case NODE_PARAMDEF:
           case NODE_PARAMETER:
                   /* More often, these appear inside NODE_FUNCPROTOTYPE. */
                   macro_open(f, "Fa");
                   macro_addnode(f, n, ARG_SPACE | ARG_SINGLE);
                   pnode_unlinksub(n);
                   break;
           case NODE_QUOTE:
                   macro_open(f, "Qo");
                   break;
           case NODE_PROGRAMLISTING:
           case NODE_SCREEN:
           case NODE_SYNOPSIS:
                   macro_line(f, "Bd -literal");
                   break;
           case NODE_REFENTRYINFO:
                   /* Suppress. */
                   pnode_unlinksub(n);
                   break;
           case NODE_REFNAME:
                   /* Suppress non-text children... */
                   macro_open(f, "Nm");
                   macro_addnode(f, n, ARG_SPACE | ARG_SINGLE);
                   pnode_unlinksub(n);
                   break;
           case NODE_REFNAMEDIV:
                   macro_line(f, "Sh NAME");
                   break;
           case NODE_REFPURPOSE:
                   macro_open(f, "Nd");
                   break;
           case NODE_REFSYNOPSISDIV:
                   pnode_printrefsynopsisdiv(f, n);
                   break;
           case NODE_PREFACE:
           case NODE_SECTION:
           case NODE_APPENDIX:
           case NODE_LEGALNOTICE:
           case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                   pnode_printrefsect(f, n);
                   break;
           case NODE_REPLACEABLE:
                   macro_open(f, "Ar");
                   break;
           case NODE_SBR:
                   macro_line(f, "br");
                   break;
           case NODE_TEXT:
           case NODE_ESCAPE:
                   pnode_printtext(f, n);
                   break;
           case NODE_TGROUP:
                   pnode_printtgroup(f, n);
                   break;
           case NODE_TITLE:
                   if (n->parent != NULL &&
                       n->parent->node == NODE_BOOKINFO) {
                           macro_open(f, "Nd");
                           break;
                 }                  }
                 if ('\n' != last)                  pnode_printpara(f, n);
                         putchar('\n');                  macro_nodeline(f, "Sy", n, 0);
                   pnode_unlinksub(n);
                 break;                  break;
           case NODE_TYPE:
                   macro_open(f, "Vt");
                   break;
           case NODE_VARIABLELIST:
                   pnode_printvariablelist(f, n);
                   break;
           case NODE_VARNAME:
                   macro_open(f, "Va");
                   break;
         default:          default:
                 break;                  break;
         }          }
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(nc, &n->childq, child)
                 pnode_print(p, pp);                  pnode_print(f, nc);
   
         switch (pn->node) {          switch (n->node) {
         case (NODE_PROGRAMLISTING):          case NODE_ESCAPE:
                 puts(".Ed");          case NODE_TERM:
           case NODE_TEXT:
                   /* Accept more arguments to the previous macro. */
                   return;
           case NODE_INFORMALEQUATION:
                   macro_line(f, "EN");
                 break;                  break;
           case NODE_INLINEEQUATION:
                   fputs("$ ", stdout);
                   f->linestate = sv;
                   break;
           case NODE_MEMBER:
                   if ((nn = TAILQ_NEXT(n, child)) != NULL &&
                       nn->node != NODE_MEMBER)
                           nn = NULL;
                   switch (f->linestate) {
                   case LINE_TEXT:
                           if (nn != NULL)
                                   print_text(f, ",", 0);
                           break;
                   case LINE_MACRO:
                           if (nn != NULL)
                                   macro_addarg(f, ",", ARG_SPACE);
                           macro_close(f);
                           break;
                   case LINE_NEW:
                           break;
                   }
                   break;
           case NODE_MML_MROW:
           case NODE_MML_MI:
           case NODE_MML_MN:
           case NODE_MML_MO:
                   if (TAILQ_EMPTY(&n->childq))
                           break;
                   fputs(" } ", stdout);
                   break;
           case NODE_QUOTE:
                   if (sv == LINE_NEW)
                           macro_close(f);
                   sv = f->linestate;
                   macro_open(f, "Qc");
                   if (sv == LINE_NEW)
                           macro_close(f);
                   break;
           case NODE_REFNAME:
                   /*
                    * If we're in the NAME macro and we have multiple
                    * <refname> macros in sequence, then print out a
                    * trailing comma before the newline.
                    */
                   if (n->parent != NULL &&
                       n->parent->node == NODE_REFNAMEDIV &&
                       TAILQ_NEXT(n, child) != NULL &&
                       TAILQ_NEXT(n, child)->node == NODE_REFNAME)
                           macro_addarg(f, ",", ARG_SPACE);
                   if (sv == LINE_NEW)
                           macro_close(f);
                   break;
           case NODE_PREFACE:
           case NODE_SECTION:
           case NODE_APPENDIX:
           case NODE_LEGALNOTICE:
           case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                   f->level--;
                   break;
           case NODE_BLOCKQUOTE:
           case NODE_LITERALLAYOUT:
           case NODE_PROGRAMLISTING:
           case NODE_SCREEN:
           case NODE_SYNOPSIS:
                   macro_line(f, "Ed");
                   break;
           case NODE_TITLE:
                   if (n->parent != NULL &&
                       n->parent->node == NODE_BOOKINFO)
                           macro_line(f, "Sh AUTHORS");
                   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(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);
           pnode_print(&formatter, tree->root);
         XML_SetCharacterDataHandler(xp, xml_char);          if (formatter.linestate != LINE_NEW)
         XML_SetElementHandler(xp, xml_elem_start, xml_elem_end);                  putchar('\n');
         XML_SetUserData(xp, &p);  
   
         while ((ssz = read(fd, b, bsz)) >= 0) {  
                 if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz)))  
                         fprintf(stderr, "%s: %s\n", fn,  
                                 XML_ErrorString  
                                 (XML_GetErrorCode(xp)));  
                 else if ( ! p.stop && ssz > 0)  
                         continue;  
                 /*  
                  * Exit when we've read all or errors have occured  
                  * during the parse sequence.  
                  */  
                 pnode_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.8  
changed lines
  Added in v.1.109

CVSweb