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

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.4 and 1.72

version 1.4, 2014/03/28 10:00:40 version 1.72, 2019/03/25 17:28:32
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 24 
Line 25 
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   #include <unistd.h>
   
 /*  #include "extern.h"
  * All recognised node types.  
  */  enum    linestate {
 enum    nodeid {          LINE_NEW = 0,
         NODE_ROOT = 0, /* Must comes first. */          LINE_TEXT,
         /* Alpha-ordered hereafter. */          LINE_MACRO
         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_SYNOPSIS,  
         NODE_TEXT,  
         NODE_TITLE,  
         NODE__MAX  
 };  };
   
 /*  /*
Line 69  enum nodeid {
Line 40  enum nodeid {
  * Keep this as simple and small as possible.   * Keep this as simple and small as possible.
  */   */
 struct  parse {  struct  parse {
           XML_Parser       xml;
         enum nodeid      node; /* current (NODE_ROOT if pre-tree) */          enum nodeid      node; /* current (NODE_ROOT if pre-tree) */
           const char      *fname; /* filename */
         int              stop; /* should we stop now? */          int              stop; /* should we stop now? */
   #define PARSE_EQN        1
           unsigned int     flags; /* document-wide flags */
         struct pnode    *root; /* root of parse tree */          struct pnode    *root; /* root of parse tree */
         struct pnode    *cur; /* current node in tree */          struct pnode    *cur; /* current node in tree */
         char            *b;          int              level; /* header level, starting at 1 */
         size_t           bsz;          enum linestate   linestate;
         size_t           mbsz;  
 };  };
   
 struct  node {  struct  node {
         const char      *name;          const char      *name; /* docbook element name */
         unsigned int     flags;          enum nodeid      node; /* docbook element to generate */
 #define NODE_IGNTEXT     1 /* ignore all contained text */  
 };  };
   
 TAILQ_HEAD(pnodeq, pnode);  TAILQ_HEAD(pnodeq, pnode);
   TAILQ_HEAD(pattrq, pattr);
   
   struct  pattr {
           enum attrkey     key;
           enum attrval     val;
           char            *rawval;
           TAILQ_ENTRY(pattr) child;
   };
   
 struct  pnode {  struct  pnode {
         enum nodeid      node; /* node type */          enum nodeid      node; /* node type */
         char            *b; /* binary data buffer */          char            *b; /* binary data buffer */
           char            *real; /* store for "b" */
         size_t           bsz; /* data buffer size */          size_t           bsz; /* data buffer size */
         struct pnode    *parent; /* parent (or NULL if top) */          struct pnode    *parent; /* parent (or NULL if top) */
         struct pnodeq    childq; /* queue of children */          struct pnodeq    childq; /* queue of children */
           struct pattrq    attrq; /* attributes of node */
         TAILQ_ENTRY(pnode) child;          TAILQ_ENTRY(pnode) child;
 };  };
   
 static  const struct node nodes[NODE__MAX] = {  static  const char *attrkeys[ATTRKEY__MAX] = {
         { NULL, 0 },          "choice",
         { "arg", 0 },          "class",
         { "citerefentry", NODE_IGNTEXT },          "close",
         { "cmdsynopsis", NODE_IGNTEXT },          "id",
         { "code", 0 },          "linkend",
         { "command", 0 },          "open",
         { "funcdef", 0 },          "rep"
         { "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 },  
         { "synopsis", 0 },  
         { NULL, 0 },  
         { "title", 0 },  
 };  };
   
 /*  static  const char *attrvals[ATTRVAL__MAX] = {
  * Look up whether "parent" is a valid parent for "node".          "monospaced",
  */          "norepeat",
 static int          "opt",
 isparent(enum nodeid node, enum nodeid parent)          "plain",
 {          "repeat",
           "req"
   };
   
         switch (node) {  static  const struct node nodes[] = {
         case (NODE_ROOT):          { "acronym",            NODE_ACRONYM },
                 return(0);          { "affiliation",        NODE_AFFILIATION },
         case (NODE_ARG):          { "anchor",             NODE_ANCHOR },
                 switch (parent) {          { "application",        NODE_APPLICATION },
                 case (NODE_ARG):          { "arg",                NODE_ARG },
                 case (NODE_CMDSYNOPSIS):          { "author",             NODE_AUTHOR },
                         return(1);          { "authorgroup",        NODE_AUTHORGROUP },
                 default:          { "blockquote",         NODE_BLOCKQUOTE },
                         break;          { "book",               NODE_BOOK },
                 }          { "bookinfo",           NODE_BOOKINFO },
                 return(0);          { "caution",            NODE_CAUTION },
         case (NODE_CITEREFENTRY):          { "chapter",            NODE_SECTION },
                 switch (parent) {          { "citerefentry",       NODE_CITEREFENTRY },
                 case (NODE_FUNCSYNOPSISINFO):          { "citetitle",          NODE_CITETITLE },
                 case (NODE_PARA):          { "cmdsynopsis",        NODE_CMDSYNOPSIS },
                 case (NODE_PROGRAMLISTING):          { "code",               NODE_CODE },
                 case (NODE_REFDESCRIPTOR):          { "colspec",            NODE_COLSPEC },
                 case (NODE_REFENTRYTITLE):          { "command",            NODE_COMMAND },
                 case (NODE_REFNAME):          { "constant",           NODE_CONSTANT },
                 case (NODE_REFPURPOSE):          { "copyright",          NODE_COPYRIGHT },
                 case (NODE_SYNOPSIS):          { "date",               NODE_DATE },
                 case (NODE_TITLE):          { "editor",             NODE_EDITOR },
                         return(1);          { "email",              NODE_EMAIL },
                 default:          { "emphasis",           NODE_EMPHASIS },
                         break;          { "entry",              NODE_ENTRY },
                 }          { "envar",              NODE_ENVAR },
                 return(0);          { "fieldsynopsis",      NODE_FIELDSYNOPSIS },
         case (NODE_CMDSYNOPSIS):          { "filename",           NODE_FILENAME },
                 switch (parent) {          { "firstname",          NODE_FIRSTNAME },
                 case (NODE_PARA):          { "firstterm",          NODE_FIRSTTERM },
                 case (NODE_REFSECT1):          { "footnote",           NODE_FOOTNOTE },
                 case (NODE_REFSYNOPSISDIV):          { "funcdef",            NODE_FUNCDEF },
                         return(1);          { "funcprototype",      NODE_FUNCPROTOTYPE },
                 default:          { "funcsynopsis",       NODE_FUNCSYNOPSIS },
                         break;          { "funcsynopsisinfo",   NODE_FUNCSYNOPSISINFO },
                 }          { "function",           NODE_FUNCTION },
                 return(0);          { "glossterm",          NODE_GLOSSTERM },
         case (NODE_CODE):          { "group",              NODE_GROUP },
                 switch (parent) {          { "holder",             NODE_HOLDER },
                 case (NODE_FUNCSYNOPSISINFO):          { "index",              NODE_INDEX },
                 case (NODE_PARA):          { "indexterm",          NODE_INDEXTERM },
                 case (NODE_PROGRAMLISTING):          { "info",               NODE_INFO },
                 case (NODE_REFDESCRIPTOR):          { "informalequation",   NODE_INFORMALEQUATION },
                 case (NODE_REFENTRYTITLE):          { "informaltable",      NODE_INFORMALTABLE },
                 case (NODE_REFNAME):          { "inlineequation",     NODE_INLINEEQUATION },
                 case (NODE_REFPURPOSE):          { "itemizedlist",       NODE_ITEMIZEDLIST },
                 case (NODE_SYNOPSIS):          { "keysym",             NODE_KEYSYM },
                 case (NODE_TITLE):          { "legalnotice",        NODE_LEGALNOTICE },
                         return(1);          { "link",               NODE_LINK },
                 default:          { "listitem",           NODE_LISTITEM },
                         break;          { "literal",            NODE_LITERAL },
                 }          { "literallayout",      NODE_LITERALLAYOUT },
                 return(0);          { "manvolnum",          NODE_MANVOLNUM },
         case (NODE_COMMAND):          { "member",             NODE_MEMBER },
                 switch (parent) {          { "mml:math",           NODE_MML_MATH },
                 case (NODE_CMDSYNOPSIS):          { "mml:mfenced",        NODE_MML_MFENCED },
                 case (NODE_FUNCSYNOPSISINFO):          { "mml:mfrac",          NODE_MML_MFRAC },
                 case (NODE_PARA):          { "mml:mi",             NODE_MML_MI },
                 case (NODE_PROGRAMLISTING):          { "mml:mn",             NODE_MML_MN },
                 case (NODE_REFDESCRIPTOR):          { "mml:mo",             NODE_MML_MO },
                 case (NODE_REFENTRYTITLE):          { "mml:mrow",           NODE_MML_MROW },
                 case (NODE_REFNAME):          { "mml:msub",           NODE_MML_MSUB },
                 case (NODE_REFPURPOSE):          { "mml:msup",           NODE_MML_MSUP },
                 case (NODE_SYNOPSIS):          { "modifier",           NODE_MODIFIER },
                 case (NODE_TITLE):          { "note",               NODE_NOTE },
                         return(1);          { "option",             NODE_OPTION },
                 default:          { "orderedlist",        NODE_ORDEREDLIST },
                         break;          { "orgname",            NODE_ORGNAME },
                 }          { "othername",          NODE_OTHERNAME },
                 return(0);          { "para",               NODE_PARA },
         case (NODE_FUNCDEF):          { "paramdef",           NODE_PARAMDEF },
                 return(NODE_FUNCPROTOTYPE == parent);          { "parameter",          NODE_PARAMETER },
         case (NODE_FUNCPROTOTYPE):          { "part",               NODE_SECTION },
                 return(NODE_FUNCSYNOPSIS == parent);          { "personname",         NODE_PERSONNAME },
         case (NODE_FUNCSYNOPSIS):          { "phrase",             NODE_PHRASE },
                 switch (parent) {          { "preface",            NODE_PREFACE },
                 case (NODE_PARA):          { "primary",            NODE_PRIMARY },
                 case (NODE_REFSECT1):          { "programlisting",     NODE_PROGRAMLISTING },
                 case (NODE_REFSYNOPSISDIV):          { "prompt",             NODE_PROMPT },
                         return(1);          { "quote",              NODE_QUOTE },
                 default:          { "refclass",           NODE_REFCLASS },
                         break;          { "refdescriptor",      NODE_REFDESCRIPTOR },
                 }          { "refentry",           NODE_REFENTRY },
                 return(0);          { "refentryinfo",       NODE_REFENTRYINFO },
         case (NODE_FUNCSYNOPSISINFO):          { "refentrytitle",      NODE_REFENTRYTITLE },
                 return(NODE_FUNCSYNOPSIS == parent);          { "refmeta",            NODE_REFMETA },
         case (NODE_FUNCTION):          { "refmetainfo",        NODE_REFMETAINFO },
                 switch (parent) {          { "refmiscinfo",        NODE_REFMISCINFO },
                 case (NODE_CODE):          { "refname",            NODE_REFNAME },
                 case (NODE_FUNCDEF):          { "refnamediv",         NODE_REFNAMEDIV },
                 case (NODE_FUNCSYNOPSISINFO):          { "refpurpose",         NODE_REFPURPOSE },
                 case (NODE_PARA):          { "refsect1",           NODE_SECTION },
                 case (NODE_PROGRAMLISTING):          { "refsect2",           NODE_SECTION },
                 case (NODE_REFDESCRIPTOR):          { "refsect3",           NODE_SECTION },
                 case (NODE_REFENTRYTITLE):          { "refsection",         NODE_SECTION },
                 case (NODE_REFNAME):          { "refsynopsisdiv",     NODE_REFSYNOPSISDIV },
                 case (NODE_REFPURPOSE):          { "releaseinfo",        NODE_RELEASEINFO },
                 case (NODE_SYNOPSIS):          { "replaceable",        NODE_REPLACEABLE },
                 case (NODE_TITLE):          { "row",                NODE_ROW },
                         return(1);          { "sbr",                NODE_SBR },
                 default:          { "screen",             NODE_SCREEN },
                         break;          { "secondary",          NODE_SECONDARY },
                 }          { "sect1",              NODE_SECTION },
                 return(0);          { "sect2",              NODE_SECTION },
         case (NODE_MANVOLNUM):          { "section",            NODE_SECTION },
                 switch (parent) {          { "sgmltag",            NODE_SGMLTAG },
                 case (NODE_CITEREFENTRY):          { "simplelist",         NODE_SIMPLELIST },
                 case (NODE_REFMETA):          { "spanspec",           NODE_SPANSPEC },
                         return(1);          { "structname",         NODE_STRUCTNAME },
                 default:          { "subtitle",           NODE_SUBTITLE },
                         break;          { "surname",            NODE_SURNAME },
                 }          { "synopsis",           NODE_SYNOPSIS },
                 return(0);          { "table",              NODE_TABLE },
         case (NODE_OPTION):          { "tbody",              NODE_TBODY },
                 switch (parent) {          { "term",               NODE_TERM },
                 case (NODE_ARG):          { "tfoot",              NODE_TFOOT },
                 case (NODE_FUNCSYNOPSISINFO):          { "tgroup",             NODE_TGROUP },
                 case (NODE_PARA):          { "thead",              NODE_THEAD },
                 case (NODE_PROGRAMLISTING):          { "tip",                NODE_TIP },
                 case (NODE_REFDESCRIPTOR):          { "title",              NODE_TITLE },
                 case (NODE_REFENTRYTITLE):          { "trademark",          NODE_TRADEMARK },
                 case (NODE_REFNAME):          { "type",               NODE_TYPE },
                 case (NODE_REFPURPOSE):          { "ulink",              NODE_ULINK },
                 case (NODE_SYNOPSIS):          { "userinput",          NODE_USERINPUT },
                 case (NODE_TITLE):          { "variablelist",       NODE_VARIABLELIST },
                         return(1);          { "varlistentry",       NODE_VARLISTENTRY },
                 default:          { "varname",            NODE_VARNAME },
                         break;          { "warning",            NODE_WARNING },
                 }          { "wordasword",         NODE_WORDASWORD },
                 return(0);          { "year",               NODE_YEAR },
         case (NODE_PARA):          { NULL,                 NODE__MAX }
                 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_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;  
         }  
   
         abort();  static  int warn = 0;
         return(0);  
 }  
   
 static void  static void
   pnode_print(struct parse *p, struct pnode *pn);
   
   /*
    * Process a stream of characters.
    * We store text as nodes in and of themselves.
    * If a text node is already open, append to it.
    * If it's not open, open one under the current context.
    */
   static void
 xml_char(void *arg, const XML_Char *p, int sz)  xml_char(void *arg, const XML_Char *p, int sz)
 {  {
         struct parse    *ps = arg;          struct parse    *ps = arg;
Line 370  xml_char(void *arg, const XML_Char *p, int sz)
Line 250  xml_char(void *arg, const XML_Char *p, int sz)
         int              i;          int              i;
   
         /* Stopped or no tree yet. */          /* Stopped or no tree yet. */
         if (ps->stop || NODE_ROOT == ps->node)          if (ps->stop || ps->node == NODE_ROOT)
                 return;                  return;
   
         /* Not supposed to be collecting text. */          assert(ps->cur != NULL);
         assert(NULL != ps->cur);  
         if (NODE_IGNTEXT & nodes[ps->node].flags)  
                 return;  
   
         /*          /*
          * Are we in the midst of processing text?           * Are we in the midst of processing text?
          * If we're not processing text right now, then create a text           * If we're not processing text right now, then create a text
          * node for doing so.           * node for doing so.
          * However, don't do so unless we have some non-whitespace to           * However, don't do so unless we have some non-whitespace to
          * process!           * process: strip out all leading whitespace to be sure.
          */           */
         if (NODE_TEXT != ps->node) {          if (ps->node != NODE_TEXT) {
                 for (i = 0; i < sz; i++)                  for (i = 0; i < sz; i++)
                         if ( ! isspace((int)p[i]))                          if ( ! isspace((unsigned char)p[i]))
                                 break;                                  break;
                 if (i == sz)                  if (i == sz)
                         return;                          return;
                 dat = calloc(1, sizeof(struct pnode));                  p += i;
                 if (NULL == dat) {                  sz -= i;
                   dat = calloc(1, sizeof(*dat));
                   if (dat == NULL) {
                         perror(NULL);                          perror(NULL);
                         exit(EXIT_FAILURE);                          exit(1);
                 }                  }
   
                 dat->node = ps->node = NODE_TEXT;                  dat->node = ps->node = NODE_TEXT;
                 dat->parent = ps->cur;                  dat->parent = ps->cur;
                 TAILQ_INIT(&dat->childq);                  TAILQ_INIT(&dat->childq);
                   TAILQ_INIT(&dat->attrq);
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);                  TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
                 ps->cur = dat;                  ps->cur = dat;
                 assert(NULL != ps->root);                  assert(ps->root != NULL);
   
         }          }
   
         /* Append to current buffer. */          /* Append to current buffer. */
         assert(sz >= 0);          assert(sz >= 0);
         ps->cur->b = realloc(ps->cur->b,          ps->cur->b = realloc(ps->cur->b, ps->cur->bsz + sz + 1);
                 ps->cur->bsz + (size_t)sz);          if (ps->cur->b == NULL) {
         if (NULL == ps->cur->b) {  
                 perror(NULL);                  perror(NULL);
                 exit(EXIT_FAILURE);                  exit(1);
         }          }
         memcpy(ps->cur->b + ps->cur->bsz, p, sz);          memcpy(ps->cur->b + ps->cur->bsz, p, sz);
         ps->cur->bsz += (size_t)sz;          ps->cur->bsz += sz;
           ps->cur->b[ps->cur->bsz] = '\0';
           ps->cur->real = ps->cur->b;
 }  }
   
   static void
   pnode_trim(struct pnode *pn)
   {
           assert(pn->node == NODE_TEXT);
           for (; pn->bsz > 0; pn->b[--pn->bsz] = '\0')
                   if ( ! isspace((unsigned char)pn->b[pn->bsz - 1]))
                           break;
   }
   
 /*  /*
  * Begin an element.   * Begin an element.
  * First, look for the 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 not parsing, keep going.
  * If we don't find it (and we're parsing), puke and exit.   * 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   * If we find it but we're not parsing yet (i.e., it's not a refentry
  * and thus out of context), keep going.   * and thus out of context), keep going.
  * If we're at the root and already have a tree, puke and exit.   * 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.   * Make sure that the element is in the right context.
  * Lastly, put the node onto our parse tree and continue.   * 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)  xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts)
 {  {
         struct parse    *ps = arg;          struct parse     *ps = arg;
         enum nodeid      node;          const struct node *node;
         struct pnode    *dat;          enum attrkey      key;
           enum attrval      val;
           struct pnode     *dat;
           struct pattr     *pattr;
           const XML_Char  **att;
   
         if (ps->stop)          /* FIXME: find a better way to ditch other namespaces. */
           if (ps->stop || strcmp(name, "xi:include") == 0)
                 return;                  return;
   
         /* Close out text node, if applicable... */          /* Close out text node, if applicable... */
         if (NODE_TEXT == ps->node) {          if (ps->node == NODE_TEXT) {
                 assert(NULL != ps->cur);                  pnode_trim(ps->cur);
                 ps->cur = ps->cur->parent;                  ps->cur = ps->cur->parent;
                 assert(NULL != ps->cur);  
                 ps->node = ps->cur->node;                  ps->node = ps->cur->node;
         }          }
   
         for (node = 0; node < NODE__MAX; node++)          for (node = nodes; node->name != NULL; node++)
                 if (NULL == nodes[node].name)                  if (strcmp(node->name, name) == 0)
                         continue;  
                 else if (0 == strcmp(nodes[node].name, name))  
                         break;                          break;
   
         if (NODE__MAX == node && NODE_ROOT == ps->node) {          if (node->name == NULL) {
                 fprintf(stderr, "%s: ignoring node\n", name);                  if (ps->node == NODE_ROOT)
                 return;                          return;
         } else if (NODE__MAX == node) {                  fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n",
                 fprintf(stderr, "%s: unknown node\n", name);                          ps->fname, XML_GetCurrentLineNumber(ps->xml),
                           XML_GetCurrentColumnNumber(ps->xml), name);
                 ps->stop = 1;                  ps->stop = 1;
                 return;                  return;
         } else if (NODE_ROOT == ps->node && NULL != ps->root) {          } else if (ps->node == NODE_ROOT && ps->root != NULL) {
                 fprintf(stderr, "%s: reentering?\n", name);                  fprintf(stderr, "%s:%zu:%zu: multiple refentries\n",
                           ps->fname, XML_GetCurrentLineNumber(ps->xml),
                           XML_GetCurrentColumnNumber(ps->xml));
                 ps->stop = 1;                  ps->stop = 1;
                 return;                  return;
         } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {  
                 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;  
         }          }
   
         if (NULL == (dat = calloc(1, sizeof(struct pnode)))) {          if (node->node == NODE_INLINEEQUATION)
                   ps->flags |= PARSE_EQN;
   
           if ((dat = calloc(1, sizeof(*dat))) == NULL) {
                 perror(NULL);                  perror(NULL);
                 exit(EXIT_FAILURE);                  exit(1);
         }          }
   
         dat->node = ps->node = node;          dat->node = ps->node = node->node;
         dat->parent = ps->cur;          dat->parent = ps->cur;
         TAILQ_INIT(&dat->childq);          TAILQ_INIT(&dat->childq);
           TAILQ_INIT(&dat->attrq);
   
         if (NULL != ps->cur)          if (ps->cur != NULL)
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);                  TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
   
         ps->cur = dat;          ps->cur = dat;
         if (NULL == ps->root)          if (ps->root == NULL)
                 ps->root = dat;                  ps->root = dat;
   
           /*
            * Process attributes.
            */
           for (att = atts; *att != NULL; att += 2) {
                   for (key = 0; key < ATTRKEY__MAX; key++)
                           if (strcmp(*att, attrkeys[key]) == 0)
                                   break;
                   if (key == ATTRKEY__MAX) {
                           if (warn)
                                   fprintf(stderr, "%s:%zu:%zu: warning: "
                                           "unknown attribute \"%s\"\n",
                                           ps->fname,
                                           XML_GetCurrentLineNumber(ps->xml),
                                           XML_GetCurrentColumnNumber(ps->xml),
                                           *att);
                           continue;
                   }
                   for (val = 0; val < ATTRVAL__MAX; val++)
                           if (strcmp(att[1], attrvals[val]) == 0)
                                   break;
                   pattr = calloc(1, sizeof(*pattr));
                   pattr->key = key;
                   pattr->val = val;
                   if (val == ATTRVAL__MAX)
                           pattr->rawval = strdup(att[1]);
                   TAILQ_INSERT_TAIL(&dat->attrq, pattr, child);
           }
   
 }  }
   
 /*  /*
  * Roll up the parse tree.   * Roll up the parse tree.
  * Does nothing else special.   * If we're at a text node, roll that one up first.
  * If we hit the root, then assign ourselves as the NODE_ROOT.   * If we hit the root, then assign ourselves as the NODE_ROOT.
  */   */
 static void  static void
Line 500  xml_elem_end(void *arg, const XML_Char *name)
Line 423  xml_elem_end(void *arg, const XML_Char *name)
 {  {
         struct parse    *ps = arg;          struct parse    *ps = arg;
   
         if (ps->stop || NODE_ROOT == ps->node)          /* FIXME: find a better way to ditch other namespaces. */
           if (ps->stop || ps->node == NODE_ROOT)
                 return;                  return;
           else if (strcmp(name, "xi:include") == 0)
                   return;
   
         /* Close out text node, if applicable... */          /* Close out text node, if applicable... */
         if (NODE_TEXT == ps->node) {          if (ps->node == NODE_TEXT) {
                 assert(NULL != ps->cur);                  pnode_trim(ps->cur);
                 ps->cur = ps->cur->parent;                  ps->cur = ps->cur->parent;
                 assert(NULL != ps->cur);  
                 ps->node = ps->cur->node;                  ps->node = ps->cur->node;
         }          }
   
         if (NULL == (ps->cur = ps->cur->parent))          if ((ps->cur = ps->cur->parent) == NULL)
                 ps->node = NODE_ROOT;                  ps->node = NODE_ROOT;
         else          else
                 ps->node = ps->cur->node;                  ps->node = ps->cur->node;
 }  }
   
   /*
    * Recursively free a node (NULL is ok).
    */
 static void  static void
 pnode_free(struct pnode *pn)  pnode_free(struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           struct pattr    *ap;
   
         if (NULL == pn)          if (pn == NULL)
                 return;                  return;
   
         while (NULL != (pp = TAILQ_FIRST(&pn->childq))) {          while ((pp = TAILQ_FIRST(&pn->childq)) != NULL) {
                 TAILQ_REMOVE(&pn->childq, pp, child);                  TAILQ_REMOVE(&pn->childq, pp, child);
                 pnode_free(pp);                  pnode_free(pp);
         }          }
   
         free(pn->b);          while ((ap = TAILQ_FIRST(&pn->attrq)) != NULL) {
                   TAILQ_REMOVE(&pn->attrq, ap, child);
                   free(ap->rawval);
                   free(ap);
           }
   
           free(pn->real);
         free(pn);          free(pn);
 }  }
   
   /*
    * Unlink a node from its parent and pnode_free() it.
    */
 static void  static void
 pnode_unlink(struct pnode *pn)  pnode_unlink(struct pnode *pn)
 {  {
           if (pn->parent != NULL)
         if (NULL != pn->parent)  
                 TAILQ_REMOVE(&pn->parent->childq, pn, child);                  TAILQ_REMOVE(&pn->parent->childq, pn, child);
         pnode_free(pn);          pnode_free(pn);
 }  }
   
   /*
    * Unlink all children of a node and pnode_free() them.
    */
 static void  static void
 pnode_unlinksub(struct pnode *pn)  pnode_unlinksub(struct pnode *pn)
 {  {
Line 551  pnode_unlinksub(struct pnode *pn)
Line 491  pnode_unlinksub(struct pnode *pn)
                 pnode_unlink(TAILQ_FIRST(&pn->childq));                  pnode_unlink(TAILQ_FIRST(&pn->childq));
 }  }
   
 static void  /*
 bufclear(struct parse *p)   * Retrieve an enumeration attribute from a node.
    * Return ATTRVAL__MAX if the node has no such attribute.
    */
   enum attrval
   pnode_getattr(struct pnode *pn, enum attrkey key)
 {  {
           struct pattr    *ap;
   
         p->b[p->bsz = 0] = '\0';          TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ap->key == key)
                           return ap->val;
           return ATTRVAL__MAX;
 }  }
   
 static void  /*
 bufappend(struct parse *p, struct pnode *pn)   * Retrieve an attribute string from a node.
    * Return defval if the node has no such attribute.
    */
   const char *
   pnode_getattr_raw(struct pnode *pn, enum attrkey key, const char *defval)
 {  {
           struct pattr    *ap;
   
         assert(NODE_TEXT == pn->node);          TAILQ_FOREACH(ap, &pn->attrq, child)
         if (p->bsz + pn->bsz + 1 > p->mbsz) {                  if (ap->key == key)
                 p->mbsz = p->bsz + pn->bsz + 1;                          return ap->val == ATTRVAL__MAX ? ap->rawval :
                 if (NULL == (p->b = realloc(p->b, p->mbsz))) {                              attrvals[ap->val];
                         perror(NULL);          return defval;
                         exit(EXIT_FAILURE);  }
                 }  
   /*
    * 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 (res != NULL)
                           break;
         }          }
         memcpy(p->b + p->bsz, pn->b, pn->bsz);  
         p->bsz += pn->bsz;          return res;
         p->b[p->bsz] = '\0';  
 }  }
   
 static void  static void
 bufappend_r(struct parse *p, struct pnode *pn)  macro_open(struct parse *p, const char *name)
 {  {
         struct pnode    *pp;          switch (p->linestate) {
           case LINE_TEXT:
                   putchar('\n');
                   /* FALLTHROUGH */
           case LINE_NEW:
                   putchar('.');
                   p->linestate = LINE_MACRO;
                   break;
           case LINE_MACRO:
                   putchar(' ');
                   break;
           }
           fputs(name, stdout);
   }
   
         if (NODE_TEXT == pn->node)  static void
                 bufappend(p, pn);  macro_close(struct parse *p)
         TAILQ_FOREACH(pp, &pn->childq, child)  {
                 bufappend_r(p, pp);          assert(p->linestate == LINE_MACRO);
           putchar('\n');
           p->linestate = LINE_NEW;
 }  }
   
   static void
   macro_line(struct parse *p, const char *name)
   {
           macro_open(p, name);
           macro_close(p);
   }
   
   #define ARG_SPACE       1  /* Insert whitespace before this argument. */
   #define ARG_SINGLE      2  /* Quote argument if it contains whitespace. */
   #define ARG_QUOTED      4  /* We are already in a quoted argument. */
   #define ARG_UPPER       8  /* Covert argument to upper case. */
 /*  /*
  * Print text presumably on a macro line.   * Print an argument string on a macro line, collapsing whitespace.
  * Ignore any child macros.  
  * Convert all whitespace to regular spaces.  
  */   */
 static void  static void
 pnode_printmacrolinepart(struct parse *p, struct pnode *pn)  macro_addarg(struct parse *p, const char *arg, int flags)
 {  {
         char            *cp;          const char      *cp;
   
         bufclear(p);          assert(p->linestate == LINE_MACRO);
         bufappend_r(p, pn);  
   
         /* Convert all space to spaces. */          /* Quote if requested and necessary. */
         for (cp = p->b; '\0' != *cp; cp++)  
                 if (isspace((int)*cp))  
                         *cp = ' ';  
   
         for (cp = p->b; isspace((int)*cp); cp++)          if ((flags & (ARG_SINGLE | ARG_QUOTED)) == ARG_SINGLE) {
                 /* Spin past whitespace (XXX: necessary?) */ ;                  for (cp = arg; *cp != '\0'; cp++)
         for ( ; '\0' != *cp; cp++) {                          if (isspace((unsigned char)*cp))
                                   break;
                   if (*cp != '\0') {
                           if (flags & ARG_SPACE) {
                                   putchar(' ');
                                   flags &= ~ ARG_SPACE;
                           }
                           putchar('"');
                           flags = ARG_QUOTED;
                   }
           }
   
           for (cp = arg; *cp != '\0'; cp++) {
   
                   /* Collapse whitespace. */
   
                   if (isspace((unsigned char)*cp)) {
                           flags |= ARG_SPACE;
                           continue;
                   } else if (flags & ARG_SPACE) {
                           putchar(' ');
                           flags &= ~ ARG_SPACE;
                   }
   
                 /* Escape us if we look like a macro. */                  /* Escape us if we look like a macro. */
                 if ((cp == p->b || ' ' == *(cp - 1)) &&  
                         isupper((int)*cp) &&                  if ((flags & ARG_QUOTED) == 0 &&
                         '\0' != *(cp + 1) &&                      (cp == arg || isspace((unsigned char)cp[-1])) &&
                         islower((int)*(cp + 1)) &&                      isupper((unsigned char)cp[0]) &&
                         ('\0' == *(cp + 2) ||                      islower((unsigned char)cp[1]) &&
                          ' ' == *(cp + 2) ||                      (cp[2] == '\0' || cp[2] == ' ' ||
                          (islower((int)*(cp + 2)) &&                       (islower((unsigned char)cp[2]) &&
                           ('\0' == *(cp + 3) ||                        (cp[3] == '\0' || cp[3] == ' '))))
                            ' ' == *(cp + 3)))))  
                         fputs("\\&", stdout);                          fputs("\\&", stdout);
                 putchar(*cp);  
                 /* If we're a character escape, escape us. */                  if (*cp == '"')
                 if ('\\' == *cp)                          fputs("\\(dq", stdout);
                   else if (flags & ARG_UPPER)
                           putchar(toupper((unsigned char)*cp));
                   else
                           putchar(*cp);
                   if (*cp == '\\')
                         putchar('e');                          putchar('e');
         }          }
 }  }
   
   static void
   macro_argline(struct parse *p, const char *name, const char *arg)
   {
           macro_open(p, name);
           macro_addarg(p, arg, ARG_SPACE);
           macro_close(p);
   }
   
 /*  /*
  * Just pnode_printmacrolinepart() but with a newline.   * Recursively append text from the children of a node to a macro line.
  * If no text, just the newline.  
  */   */
 static void  static void
 pnode_printmacroline(struct parse *p, struct pnode *pn)  macro_addnode(struct parse *p, struct pnode *pn, int flags)
 {  {
           int              quote_now;
   
         pnode_printmacrolinepart(p, pn);          assert(p->linestate == LINE_MACRO);
         putchar('\n');  
           /*
            * If the only child is a text node, just add that text,
            * letting macro_addarg() decide about quoting.
            */
   
           pn = TAILQ_FIRST(&pn->childq);
           if (pn != NULL && pn->node == NODE_TEXT &&
               TAILQ_NEXT(pn, child) == NULL) {
                   macro_addarg(p, pn->b, flags);
                   return;
           }
   
           /*
            * If we want the argument quoted and are not already
            * in a quoted context, quote now.
            */
   
           quote_now = 0;
           if (flags & ARG_SINGLE) {
                   if ((flags & ARG_QUOTED) == 0) {
                           if (flags & ARG_SPACE) {
                                   putchar(' ');
                                   flags &= ~ARG_SPACE;
                           }
                           putchar('"');
                           flags |= ARG_QUOTED;
                           quote_now = 1;
                   }
                   flags &= ~ARG_SINGLE;
           }
   
           /*
            * Iterate to child and sibling nodes,
            * inserting whitespace between nodes.
            */
   
           while (pn != NULL) {
                   if (pn->node == NODE_TEXT)
                           macro_addarg(p, pn->b, flags);
                   else
                           macro_addnode(p, pn, flags);
                   pn = TAILQ_NEXT(pn, child);
                   flags |= ARG_SPACE;
           }
           if (quote_now)
                   putchar('"');
 }  }
   
 static void  static void
   macro_nodeline(struct parse *p, const char *name, struct pnode *pn, int flags)
   {
           macro_open(p, name);
           macro_addnode(p, pn, ARG_SPACE | flags);
           macro_close(p);
   }
   
   /*
    * If the next node is a text node starting with closing punctuation,
    * emit the closing punctuation as a trailing macro argument.
    */
   static void
   macro_closepunct(struct parse *p, struct pnode *pn)
   {
           if ((pn = TAILQ_NEXT(pn, child)) != NULL &&
               pn->node == NODE_TEXT && pn->bsz > 0 &&
               (pn->b[0] == ',' || pn->b[0] == '.') &&
               (pn->bsz == 1 || isspace((unsigned char)pn->b[1]))) {
                   putchar(' ');
                   putchar(pn->b[0]);
                   pn->b++;
                   pn->bsz--;
           }
           macro_close(p);
   }
   
   static void
   print_text(struct parse *p, const char *word)
   {
           switch (p->linestate) {
           case LINE_NEW:
                   break;
           case LINE_TEXT:
                   putchar(' ');
                   break;
           case LINE_MACRO:
                   macro_close(p);
                   break;
           }
           fputs(word, stdout);
           p->linestate = LINE_TEXT;
   }
   
   static void
   pnode_printpara(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL &&
               (pp = pn->parent) == NULL)
                   return;
   
           switch (pp->node) {
           case NODE_ENTRY:
           case NODE_LISTITEM:
                   return;
           case NODE_PREFACE:
           case NODE_SECTION:
                   if (p->level < 3)
                           return;
                   break;
           default:
                   break;
           }
           macro_line(p, "Pp");
   }
   
   /*
    * If the SYNOPSIS macro has a superfluous title, kill it.
    */
   static void
   pnode_printrefsynopsisdiv(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp, *pq;
   
           TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq)
                   if (pp->node == NODE_TITLE)
                           pnode_unlink(pp);
   
           macro_line(p, "Sh SYNOPSIS");
   }
   
   /*
    * Start a hopefully-named `Sh' section.
    */
   static void
 pnode_printrefsect(struct parse *p, struct pnode *pn)  pnode_printrefsect(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           const char      *title;
           int              flags, level;
   
           if (pn->parent == NULL)
                   return;
   
           level = ++p->level;
           flags = ARG_SPACE;
           if (level == 1)
                   flags |= ARG_UPPER;
           if (level < 3) {
                   switch (pn->node) {
                   case NODE_CAUTION:
                   case NODE_NOTE:
                   case NODE_TIP:
                   case NODE_WARNING:
                           level = 3;
                           break;
                   default:
                           break;
                   }
           }
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TITLE == pp->node)                  if (pp->node == NODE_TITLE)
                         break;                          break;
   
         fputs(".Sh ", stdout);          if (pp == NULL) {
                   switch (pn->node) {
                   case NODE_PREFACE:
                           title = "Preface";
                           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;
                   }
           }
   
         if (NULL != pp)          switch (level) {
                 pnode_printmacroline(p, pp);          case 1:
         else                  macro_open(p, "Sh");
                 puts("UNKNOWN");                  break;
           case 2:
                   macro_open(p, "Ss");
                   break;
           default:
                   pnode_printpara(p, pn);
                   macro_open(p, "Sy");
                   break;
           }
   
           if (pp != NULL) {
                   macro_addnode(p, pp, flags);
                   pnode_unlink(pp);
           } else
                   macro_addarg(p, title, ARG_SPACE | ARG_QUOTED);
           macro_close(p);
 }  }
   
   /*
    * Start a reference, extracting the title and volume.
    */
 static void  static void
 pnode_printciterefentry(struct parse *p, struct pnode *pn)  pnode_printciterefentry(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *title, *manvol;          struct pnode    *pp, *title, *manvol;
   
         title = manvol = NULL;          title = manvol = NULL;
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_MANVOLNUM == pp->node)                  if (pp->node == NODE_MANVOLNUM)
                         manvol = pp;                          manvol = pp;
                 else if (NODE_REFENTRYTITLE == pp->node)                  else if (pp->node == NODE_REFENTRYTITLE)
                         title = pp;                          title = pp;
           }
         fputs(".Xr ", stdout);          macro_open(p, "Xr");
           if (title == NULL)
         if (NULL != title) {                  macro_addarg(p, "unknown", ARG_SPACE);
                 pnode_printmacrolinepart(p, title);  
                 putchar(' ');  
         } else  
                 fputs("unknown ", stdout);  
   
         if (NULL != manvol)  
                 pnode_printmacroline(p, manvol);  
         else          else
                 puts("1");                  macro_addnode(p, title, ARG_SPACE | ARG_SINGLE);
           if (manvol == NULL)
                   macro_addarg(p, "1", ARG_SPACE);
           else
                   macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE);
           macro_close(p);
           pnode_unlinksub(pn);
 }  }
   
 static void  static void
Line 686  pnode_printrefmeta(struct parse *p, struct pnode *pn)
Line 899  pnode_printrefmeta(struct parse *p, struct pnode *pn)
         struct pnode    *pp, *title, *manvol;          struct pnode    *pp, *title, *manvol;
   
         title = manvol = NULL;          title = manvol = NULL;
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_MANVOLNUM == pp->node)                  if (pp->node == NODE_MANVOLNUM)
                         manvol = pp;                          manvol = pp;
                 else if (NODE_REFENTRYTITLE == pp->node)                  else if (pp->node == NODE_REFENTRYTITLE)
                         title = pp;                          title = pp;
           }
         puts(".Dd $Mdocdate" "$");          macro_open(p, "Dt");
         fputs(".Dt ", stdout);          if (title == NULL)
                   macro_addarg(p, "UNKNOWN", ARG_SPACE);
         if (NULL != title) {  
                 pnode_printmacrolinepart(p, title);  
                 putchar(' ');  
         } else  
                 fputs("UNKNOWN ", stdout);  
   
         if (NULL != manvol)  
                 pnode_printmacroline(p, manvol);  
         else          else
                 puts("1");                  macro_addnode(p, title, ARG_SPACE | ARG_SINGLE | ARG_UPPER);
           if (manvol == NULL)
         puts(".Os");                  macro_addarg(p, "1", ARG_SPACE);
           else
                   macro_addnode(p, manvol, ARG_SPACE | ARG_SINGLE);
           macro_close(p);
           pnode_unlink(pn);
 }  }
   
 static void  static void
Line 715  pnode_printfuncdef(struct parse *p, struct pnode *pn)
Line 924  pnode_printfuncdef(struct parse *p, struct pnode *pn)
         struct pnode    *pp, *ftype, *func;          struct pnode    *pp, *ftype, *func;
   
         ftype = func = NULL;          ftype = func = NULL;
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_TEXT == pp->node)                  if (pp->node == NODE_TEXT)
                         ftype = pp;                          ftype = pp;
                 else if (NODE_FUNCTION == pp->node)                  else if (pp->node == NODE_FUNCTION)
                         func = pp;                          func = pp;
   
         if (NULL != ftype) {  
                 fputs(".Ft ", stdout);  
                 pnode_printmacroline(p, ftype);  
         }          }
           if (ftype != NULL)
         if (NULL != func) {                  macro_argline(p, "Ft", ftype->b);
                 fputs(".Fo ", stdout);          macro_open(p, "Fo");
                 pnode_printmacroline(p, func);          if (func == NULL)
         } else                  macro_addarg(p, "UNKNOWN", ARG_SPACE);
                 puts(".Fo UNKNOWN");          else
                   macro_addnode(p, func, ARG_SPACE | ARG_SINGLE);
           macro_close(p);
 }  }
   
   /*
    * 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_printparamdef(struct parse *p, struct pnode *pn)  pnode_printmathfenced(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *ptype, *param;          struct pnode    *pp;
   
         ptype = param = NULL;          printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "("));
         TAILQ_FOREACH(pp, &pn->childq, child)  
                 if (NODE_TEXT == pp->node)  
                         ptype = pp;  
                 else if (NODE_PARAMETER == pp->node)  
                         param = pp;  
   
         fputs(".Fa \"", stdout);          pp = TAILQ_FIRST(&pn->childq);
         if (NULL != ptype) {          pnode_print(p, pp);
                 pnode_printmacrolinepart(p, ptype);  
                 putchar(' ');          while ((pp = TAILQ_NEXT(pp, child)) != NULL) {
                   putchar(',');
                   pnode_print(p, pp);
         }          }
           printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")"));
           pnode_unlinksub(pn);
   }
   
         if (NULL != param)  /*
                 pnode_printmacrolinepart(p, param);   * These math nodes require special handling because they have infix
         else   * syntax, instead of the usual prefix or prefix.
                 fputs("UNKNOWN", stdout);   * So we need to break up the first and second child node with a
    * particular eqn(7) word.
    */
   static void
   pnode_printmath(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
         puts("\"");          pp = TAILQ_FIRST(&pn->childq);
           pnode_print(p, pp);
   
           switch (pn->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;
           }
   
           pp = TAILQ_NEXT(pp, child);
           pnode_print(p, pp);
           pnode_unlinksub(pn);
 }  }
   
 static void  static void
Line 765  pnode_printfuncprototype(struct parse *p, struct pnode
Line 1003  pnode_printfuncprototype(struct parse *p, struct pnode
         struct pnode    *pp, *fdef;          struct pnode    *pp, *fdef;
   
         TAILQ_FOREACH(fdef, &pn->childq, child)          TAILQ_FOREACH(fdef, &pn->childq, child)
                 if (NODE_FUNCDEF == fdef->node)                  if (fdef->node == NODE_FUNCDEF)
                         break;                          break;
   
         if (NULL != fdef)          if (fdef != NULL)
                 pnode_printfuncdef(p, fdef);                  pnode_printfuncdef(p, fdef);
         else          else
                 puts(".Fo UNKNOWN");                  macro_line(p, "Fo UNKNOWN");
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_PARAMDEF == pp->node)                  if (pp->node == NODE_PARAMDEF)
                         pnode_printparamdef(p, pp);                          macro_nodeline(p, "Fa", pp, ARG_SINGLE);
   
         puts(".Fc");          macro_line(p, "Fc");
           pnode_unlinksub(pn);
 }  }
   
 /* TODO: handle "optional" values. */  /*
    * 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, int nested)  pnode_printarg(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
         int              sv = nested;          struct pattr    *ap;
           int              isop, isrep;
   
         if ( ! nested)          isop = 1;
                 fputs(".", stdout);          isrep = 0;
         nested = 1;          TAILQ_FOREACH(ap, &pn->attrq, child) {
                   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");
   
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   if (pp->node == NODE_TEXT)
                           macro_open(p, "Ar");
                   pnode_print(p, pp);
                   if (isrep && pp->node == NODE_TEXT)
                           macro_addarg(p, "...", ARG_SPACE);
           }
           pnode_unlinksub(pn);
   }
   
   static void
   pnode_printgroup(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp, *np;
           struct pattr    *ap;
           int              isop, sv;
   
           isop = 1;
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ap->key == ATTRKEY_CHOICE &&
                       (ap->val == ATTRVAL_PLAIN || ap->val == ATTRVAL_REQ)) {
                           isop = 0;
                           break;
                   }
   
           /*
            * Make sure we're on a macro line.
            * This will prevent pnode_print() for putting us on a
            * subsequent line.
            */
           sv = p->linestate == LINE_NEW;
           if (isop)
                   macro_open(p, "Op");
           else if (sv)
                   macro_open(p, "No");
   
           /*
            * 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
   pnode_printprologue(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           pp = p->root == NULL ? NULL :
                   pnode_findfirst(p->root, NODE_REFMETA);
   
           macro_line(p, "Dd $Mdocdate" "$");
           if (pp != NULL)
                   pnode_printrefmeta(p, pp);
           else {
                   macro_open(p, "Dt");
                   macro_addarg(p,
                       pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN"),
                       ARG_SPACE | ARG_SINGLE | ARG_UPPER);
                   macro_addarg(p, "1", ARG_SPACE);
                   macro_close(p);
           }
           macro_line(p, "Os");
   
           if (p->flags & PARSE_EQN) {
                   macro_line(p, "EQ");
                   print_text(p, "delim $$");
                   macro_line(p, "EN");
           }
   }
   
   /*
    * We can have multiple <term> elements within a <varlistentry>, which
    * we should comma-separate as list headers.
    */
   static void
   pnode_printvarlistentry(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
           int              first = 1;
   
           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_OPTION == pp->node) {                  if (pp->node != NODE_TERM)
                         fputs("Fl ", stdout);                          pnode_print(p, pp);
                         pnode_printmacrolinepart(p, pp);          pnode_unlinksub(pn);
                 } else if (NODE_TEXT == pp->node) {  }
                         fputs("Ar ", stdout);  
                         pnode_printmacrolinepart(p, pp);  
                 } else if (NODE_ARG == pp->node)  
                         pnode_printarg(p, pp, nested);  
   
         if ( ! sv)  static void
                 puts("");  pnode_printtitle(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp, *pq;
   
           TAILQ_FOREACH_SAFE(pp, &pn->childq, child, pq) {
                   if (pp->node == NODE_TITLE) {
                           pnode_printpara(p, pp);
                           pnode_print(p, pp);
                           pnode_unlink(pp);
                   }
           }
 }  }
   
   static void
   pnode_printrow(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           macro_line(p, "Bl -dash -compact");
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   macro_line(p, "It");
                   pnode_print(p, pp);
           }
           macro_line(p, "El");
           pnode_unlink(pn);
   }
   
   static void
   pnode_printtable(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           pnode_printtitle(p, pn);
           macro_line(p, "Bl -ohang");
           while ((pp = pnode_findfirst(pn, NODE_ROW)) != NULL) {
                   macro_line(p, "It Table Row");
                   pnode_printrow(p, pp);
           }
           macro_line(p, "El");
           pnode_unlinksub(pn);
   }
   
   static void
   pnode_printlist(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           pnode_printtitle(p, pn);
           macro_argline(p, "Bl",
               pn->node == NODE_ORDEREDLIST ? "-enum" : "-bullet");
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   macro_line(p, "It");
                   pnode_print(p, pp);
           }
           macro_line(p, "El");
           pnode_unlinksub(pn);
   }
   
   static void
   pnode_printvariablelist(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           pnode_printtitle(p, pn);
           macro_line(p, "Bl -tag -width Ds");
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   if (pp->node == NODE_VARLISTENTRY)
                           pnode_print(p, pp);
                   else
                           macro_nodeline(p, "It", pp, 0);
           }
           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 parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           const char      *ccp;
         char            *cp;          char            *cp;
         int              last;          int              last;
           enum linestate   sv;
   
         if (NULL == pn)          if (pn == NULL)
                 return;                  return;
   
         if (NODE_TEXT != pn->node && NODE_ROOT != pn->node)          sv = p->linestate;
                 printf(".\\\" %s\n", nodes[pn->node].name);  
   
         switch (pn->node) {          switch (pn->node) {
         case (NODE_ARG):          case NODE_APPLICATION:
                 pnode_printarg(p, pn, 0);                  macro_open(p, "Nm");
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_CITEREFENTRY):          case NODE_ANCHOR:
                   /* Don't print anything! */
                   return;
           case NODE_ARG:
                   pnode_printarg(p, pn);
                   break;
           case NODE_AUTHOR:
                   macro_open(p, "An");
                   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:
                 fputs(".Li ", stdout);                  macro_open(p, "%T");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_COMMAND):          case NODE_CODE:
                 fputs(".Nm ", stdout);                  macro_open(p, "Li");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCTION):          case NODE_COMMAND:
                 fputs(".Fn ", stdout);                  macro_open(p, "Nm");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_FUNCPROTOTYPE):          case NODE_CONSTANT:
                   macro_open(p, "Dv");
                   break;
           case NODE_EDITOR:
                   print_text(p, "editor:");
                   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_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:
                 fputs(".Fd ", stdout);                  macro_open(p, "Fd");
                 pnode_printmacroline(p, pn);  
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_PARA):          case NODE_INDEXTERM:
                 /* FIXME: not always. */                  return;
                 puts(".Pp");          case NODE_INFORMALEQUATION:
                   macro_line(p, "EQ");
                 break;                  break;
         case (NODE_PARAMETER):          case NODE_INLINEEQUATION:
                 fputs(".Fa \"", stdout);                  if (p->linestate == LINE_NEW)
                 pnode_printmacrolinepart(p, pn);                          p->linestate = LINE_TEXT;
                 puts("\"");                  putchar('$');
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_ITEMIZEDLIST:
                 puts(".Bd -literal");                  pnode_printlist(p, pn);
                 break;                  break;
         case (NODE_REFMETA):          case NODE_GROUP:
                 pnode_printrefmeta(p, pn);                  pnode_printgroup(p, pn);
                   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_REFNAME):          case NODE_QUOTE:
                 fputs(".Nm ", stdout);                  macro_open(p, "Qo");
                 pnode_printmacroline(p, pn);                  break;
           case NODE_PROGRAMLISTING:
           case NODE_SCREEN:
                   macro_line(p, "Bd -literal");
                   break;
           case NODE_REFENTRYINFO:
                   /* Suppress. */
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 return;  
         case (NODE_REFNAMEDIV):  
                 puts(".Sh NAME");  
                 break;                  break;
         case (NODE_REFPURPOSE):          case NODE_REFMETA:
                 fputs(".Nd ", stdout);                  abort();
                 pnode_printmacroline(p, pn);                  break;
           case NODE_REFNAME:
                   /* Suppress non-text children... */
                   macro_open(p, "Nm");
                   macro_addnode(p, pn, ARG_SPACE | ARG_SINGLE);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 return;  
         case (NODE_REFSYNOPSISDIV):  
                 puts(".Sh SYNOPSIS");  
                 break;                  break;
         case (NODE_REFSECT1):          case NODE_REFNAMEDIV:
                   macro_line(p, "Sh NAME");
                   break;
           case NODE_REFPURPOSE:
                   macro_open(p, "Nd");
                   break;
           case NODE_REFSYNOPSISDIV:
                   pnode_printrefsynopsisdiv(p, pn);
                   break;
           case NODE_PREFACE:
           case NODE_SECTION:
           case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                 pnode_printrefsect(p, pn);                  pnode_printrefsect(p, pn);
                 pnode_unlinksub(pn);  
                 break;                  break;
         case (NODE_TEXT):          case NODE_REPLACEABLE:
                 bufclear(p);                  macro_open(p, "Ar");
                 bufappend(p, pn);                  break;
           case NODE_SBR:
                   macro_line(p, "br");
                   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.
                  */                   */
                 for (last = '\n', cp = p->b; '\0' != *cp; ) {                  cp = pn->b;
                         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');
                 }                  }
                 if ('\n' != last)  
                         putchar('\n');  
                 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 927  pnode_print(struct parse *p, struct pnode *pn)
Line 1503  pnode_print(struct parse *p, struct pnode *pn)
                 pnode_print(p, pp);                  pnode_print(p, pp);
   
         switch (pn->node) {          switch (pn->node) {
         case (NODE_PROGRAMLISTING):          case NODE_INFORMALEQUATION:
                 puts(".Ed");                  macro_line(p, "EN");
                 break;                  break;
           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
                    * <refname> macros in sequence, then print out a
                    * trailing comma before the newline.
                    */
                   if (pn->parent != NULL &&
                       pn->parent->node == NODE_REFNAMEDIV &&
                       TAILQ_NEXT(pn, child) != NULL &&
                       TAILQ_NEXT(pn, child)->node == NODE_REFNAME)
                           macro_addarg(p, ",", ARG_SPACE);
                   if (sv == LINE_NEW)
                           macro_close(p);
                   break;
           case NODE_PREFACE:
           case NODE_SECTION:
           case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                   p->level--;
                   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;
         }          }
Line 940  pnode_print(struct parse *p, struct pnode *pn)
Line 1597  pnode_print(struct parse *p, struct pnode *pn)
  * Invoke the parser context with each buffer fill.   * Invoke the parser context with each buffer fill.
  */   */
 static int  static int
 readfile(XML_Parser xp, int fd,  readfile(XML_Parser xp, int fd,
         char *b, size_t bsz, const char *fn)          char *b, size_t bsz, const char *fn)
 {  {
         struct parse     p;          struct parse     p;
Line 949  readfile(XML_Parser xp, int fd, 
Line 1606  readfile(XML_Parser xp, int fd, 
   
         memset(&p, 0, sizeof(struct parse));          memset(&p, 0, sizeof(struct parse));
   
         p.b = malloc(p.bsz = p.mbsz = 1024);          p.fname = fn;
           p.xml = xp;
   
         XML_SetCharacterDataHandler(xp, xml_char);          XML_SetCharacterDataHandler(xp, xml_char);
         XML_SetElementHandler(xp, xml_elem_start, xml_elem_end);          XML_SetElementHandler(xp, xml_elem_start, xml_elem_end);
         XML_SetUserData(xp, &p);          XML_SetUserData(xp, &p);
   
         while ((ssz = read(fd, b, bsz)) >= 0) {          while ((ssz = read(fd, b, bsz)) >= 0) {
                 if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz)))                  if ((rc = XML_Parse(xp, b, ssz, 0 == ssz)) == 0)
                         fprintf(stderr, "%s: %s\n", fn,                          fprintf(stderr, "%s:%zu:%zu: %s\n", fn,
                                   XML_GetCurrentLineNumber(xp),
                                   XML_GetCurrentColumnNumber(xp),
                                 XML_ErrorString                                  XML_ErrorString
                                 (XML_GetErrorCode(xp)));                                  (XML_GetErrorCode(xp)));
                 else if ( ! p.stop && ssz > 0)                  else if ( ! p.stop && ssz > 0)
                         continue;                          continue;
                 /*                  /*
                  * Exit when we've read all or errors have occured                   * Exit when we've read all or errors have occured
                  * during the parse sequence.                   * during the parse sequence.
                  */                   */
                   p.linestate = LINE_NEW;
                   pnode_printprologue(&p, p.root);
                 pnode_print(&p, p.root);                  pnode_print(&p, p.root);
                   if (p.linestate != LINE_NEW)
                           putchar('\n');
                 pnode_free(p.root);                  pnode_free(p.root);
                 free(p.b);                  return rc != 0 && p.stop == 0;
                 return(0 != rc && ! p.stop);  
         }          }
   
         /* Read error has occured. */          /* Read error has occured. */
         perror(fn);          perror(fn);
         pnode_free(p.root);          pnode_free(p.root);
         free(p.b);          return 0;
         return(0);  
 }  }
   
 int  int
Line 985  main(int argc, char *argv[])
Line 1647  main(int argc, char *argv[])
         XML_Parser       xp;          XML_Parser       xp;
         const char      *fname;          const char      *fname;
         char            *buf;          char            *buf;
         int              fd, rc;          int              fd, rc, ch;
           const char      *progname;
   
           progname = strrchr(argv[0], '/');
           if (progname == NULL)
                   progname = argv[0];
           else
                   ++progname;
   
         fname = "-";          fname = "-";
         xp = NULL;          xp = NULL;
         buf = NULL;          buf = NULL;
         rc = 0;          rc = 1;
   
         if (-1 != getopt(argc, argv, ""))          while ((ch = getopt(argc, argv, "W")) != -1)
                 return(EXIT_FAILURE);                  switch (ch) {
                   case 'W':
                           warn = 1;
                           break;
                   default:
                           goto usage;
                   }
   
         argc -= optind;          argc -= optind;
         argv += optind;          argv += optind;
   
         if (argc > 1)          if (argc > 1) {
                 return(EXIT_FAILURE);                  fprintf(stderr, "%s: Too many arguments\n", argv[1]);
         else if (argc > 0)                  goto usage;
           } else if (argc > 0)
                 fname = argv[0];                  fname = argv[0];
   
         /* Read from stdin or a file. */          /* Read from stdin or a file. */
         fd = 0 == strcmp(fname, "-") ?          fd = strcmp(fname, "-") == 0 ?
                 STDIN_FILENO : open(fname, O_RDONLY, 0);                  STDIN_FILENO : open(fname, O_RDONLY, 0);
   
         /*          /*
Line 1013  main(int argc, char *argv[])
Line 1689  main(int argc, char *argv[])
          * Create the parser context.           * Create the parser context.
          * Dive directly into the parse.           * Dive directly into the parse.
          */           */
         if (-1 == fd)          if (fd == -1)
                 perror(fname);                  perror(fname);
         else if (NULL == (buf = malloc(4096)))          else if ((buf = malloc(4096)) == NULL)
                 perror(NULL);                  perror(NULL);
         else if (NULL == (xp = XML_ParserCreate(NULL)))          else if ((xp = XML_ParserCreate(NULL)) == NULL)
                 perror(NULL);                  perror(NULL);
         else if ( ! readfile(xp, fd, buf, 4096, fname))          else if (readfile(xp, fd, buf, 4096, fname))
                 rc = 1;                  rc = 0;
   
         XML_ParserFree(xp);          XML_ParserFree(xp);
         free(buf);          free(buf);
         if (STDIN_FILENO != fd)          if (fd != STDIN_FILENO)
                 close(fd);                  close(fd);
         return(rc ? EXIT_SUCCESS : EXIT_FAILURE);          return rc;
   
   usage:
           fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname);
           return 1;
 }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.72

CVSweb