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

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.11 and 1.52

version 1.11, 2014/03/29 11:13:49 version 1.52, 2019/03/22 16:21:23
Line 1 
Line 1 
 /*      $Id$ */  /* $Id$ */
 /*  /*
  * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 26 
Line 27 
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
 /*  #include "extern.h"
  * All recognised node types.  
  */  
 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.   * Global parse state.
  * 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; /* nil-terminated buffer for pre-print */          char            *b; /* NUL-terminated buffer for pre-print */
         size_t           bsz; /* current length of b */          size_t           bsz; /* current length of b */
         size_t           mbsz; /* max bsz allocation */          size_t           mbsz; /* max bsz allocation */
           int              level; /* header level, starting at 1 */
         int              newln; /* output: are we on a fresh line */          int              newln; /* output: are we on a fresh line */
 };  };
   
Line 88  struct node {
Line 56  struct node {
 };  };
   
 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 char *attrkeys[ATTRKEY__MAX] = {
           "choice",
           "close",
           "id",
           "open",
           "rep"
   };
   
   static  const char *attrvals[ATTRVAL__MAX] = {
           "norepeat",
           "opt",
           "plain",
           "repeat",
           "req"
   };
   
 static  const struct node nodes[NODE__MAX] = {  static  const struct node nodes[NODE__MAX] = {
         { NULL, 0 },          { NULL, 0 },
         { "arg", 0 },          { "acronym", 0 },
         { "citerefentry", NODE_IGNTEXT },          { "affiliation", NODE_IGNTEXT },
         { "cmdsynopsis", NODE_IGNTEXT },          { "anchor", NODE_IGNTEXT },
         { "code", 0 },          { "application", 0 },
         { "command", 0 },          { "arg", 0 },
           { "author", NODE_IGNTEXT },
           { "authorgroup", NODE_IGNTEXT },
           { "blockquote", NODE_IGNTEXT },
           { "book", NODE_IGNTEXT },
           { "bookinfo", NODE_IGNTEXT },
           { "caution", NODE_IGNTEXT },
           { "chapter", NODE_IGNTEXT },
           { "citerefentry", NODE_IGNTEXT },
           { "citetitle", 0 },
           { "cmdsynopsis", NODE_IGNTEXT },
           { "code", 0 },
           { "colspec", NODE_IGNTEXT },
           { "command", 0 },
           { "constant", 0 },
           { "copyright", NODE_IGNTEXT },
           { "date", 0 },
           { "editor", NODE_IGNTEXT },
           { "emphasis", 0 },
           { "entry", 0 },
           { "envar", 0 },
           { "fieldsynopsis", NODE_IGNTEXT },
           { "filename", 0 },
           { "firstname", 0 },
           { "firstterm", 0 },
           { "footnote", 0 },
         { "funcdef", 0 },          { "funcdef", 0 },
         { "funcprototype", NODE_IGNTEXT },          { "funcprototype", NODE_IGNTEXT },
         { "funcsynopsis", NODE_IGNTEXT },          { "funcsynopsis", NODE_IGNTEXT },
         { "funcsynopsisinfo", 0 },          { "funcsynopsisinfo", 0 },
         { "function", 0 },          { "function", 0 },
         { "manvolnum", 0 },          { "glossterm", 0 },
         { "option", 0 },          { "group", NODE_IGNTEXT },
         { "para", 0 },          { "holder", NODE_IGNTEXT },
           { "index", NODE_IGNTEXT },
           { "indexterm", NODE_IGNTEXT },
           { "info", NODE_IGNTEXT },
           { "informalequation", NODE_IGNTEXT },
           { "informaltable", NODE_IGNTEXT },
           { "inlineequation", NODE_IGNTEXT },
           { "itemizedlist", NODE_IGNTEXT },
           { "keysym", 0 },
           { "legalnotice", NODE_IGNTEXT },
           { "link", 0 },
           { "listitem", NODE_IGNTEXT },
           { "literal", 0 },
           { "literallayout", 0 },
           { "manvolnum", 0 },
           { "member", 0 },
           { "mml:math", NODE_IGNTEXT },
           { "mml:mfenced", 0 },
           { "mml:mfrac", 0 },
           { "mml:mi", 0 },
           { "mml:mn", 0 },
           { "mml:mo", 0 },
           { "mml:mrow", 0 },
           { "mml:msub", 0 },
           { "mml:msup", 0 },
           { "modifier", 0 },
           { "note", NODE_IGNTEXT },
           { "option", 0 },
           { "orderedlist", NODE_IGNTEXT },
           { "orgname", 0 },
           { "othername", 0 },
           { "para", 0 },
         { "paramdef", 0 },          { "paramdef", 0 },
         { "parameter", 0 },          { "parameter", 0 },
         { "programlisting", 0 },          { "part", NODE_IGNTEXT },
         { "refclass", NODE_IGNTEXT },          { "phrase", 0 },
         { "refdescriptor", NODE_IGNTEXT },          { "preface", NODE_IGNTEXT },
         { "refentry", NODE_IGNTEXT },          { "primary", 0 },
         { "refentrytitle", 0 },          { "programlisting", 0 },
         { "refmeta", NODE_IGNTEXT },          { "prompt", 0 },
         { "refmiscinfo", NODE_IGNTEXT },          { "quote", 0 },
         { "refname", 0 },          { "refclass", NODE_IGNTEXT },
         { "refnamediv", NODE_IGNTEXT },          { "refdescriptor", NODE_IGNTEXT },
         { "refpurpose", 0 },          { "refentry", NODE_IGNTEXT },
         { "refsect1", 0 },          { "refentryinfo", NODE_IGNTEXT },
         { "refsynopsisdiv", NODE_IGNTEXT },          { "refentrytitle", 0 },
         { "structname", 0 },          { "refmeta", NODE_IGNTEXT },
         { "synopsis", 0 },          { "refmetainfo", NODE_IGNTEXT },
         { NULL, 0 },          { "refmiscinfo", NODE_IGNTEXT },
         { "title", 0 },          { "refname", 0 },
           { "refnamediv", NODE_IGNTEXT },
           { "refpurpose", 0 },
           { "refsect1", NODE_IGNTEXT },
           { "refsect2", NODE_IGNTEXT },
           { "refsect3", NODE_IGNTEXT },
           { "refsection", NODE_IGNTEXT },
           { "refsynopsisdiv", NODE_IGNTEXT },
           { "releaseinfo", 0 },
           { "replaceable", 0 },
           { "row", NODE_IGNTEXT },
           { "sbr", NODE_IGNTEXT },
           { "screen", NODE_IGNTEXT },
           { "secondary", 0 },
           { "sect1", NODE_IGNTEXT },
           { "sect2", NODE_IGNTEXT },
           { "section", NODE_IGNTEXT },
           { "sgmltag", 0 },
           { "simplelist", NODE_IGNTEXT },
           { "spanspec", NODE_IGNTEXT },
           { "structname", 0 },
           { "subtitle", 0 },
           { "surname", 0 },
           { "synopsis", 0 },
           { "table", NODE_IGNTEXT },
           { "tbody", NODE_IGNTEXT },
           { "term", 0 },
           { NULL, 0 },
           { "tfoot", NODE_IGNTEXT },
           { "tgroup", NODE_IGNTEXT },
           { "thead", NODE_IGNTEXT },
           { "tip", NODE_IGNTEXT },
           { "title", 0 },
           { "trademark", 0 },
           { "type", 0 },
           { "ulink", 0 },
           { "userinput", 0 },
           { "variablelist", NODE_IGNTEXT },
           { "varlistentry", NODE_IGNTEXT },
           { "varname", 0 },
           { "warning", NODE_IGNTEXT },
           { "wordasword", 0 },
           { "year", NODE_IGNTEXT },
 };  };
   
   static  int warn = 0;
   
 static void  static void
 pnode_print(struct parse *p, struct pnode *pn);  pnode_print(struct parse *p, struct pnode *pn);
   
 /*  /*
  * 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)  
 {  
   
         switch (node) {  
         case (NODE_ROOT):  
                 return(0);  
         case (NODE_ARG):  
                 switch (parent) {  
                 case (NODE_ARG):  
                 case (NODE_CMDSYNOPSIS):  
                         return(1);  
                 default:  
                         break;  
                 }  
                 return(0);  
         case (NODE_CITEREFENTRY):  
                 switch (parent) {  
                 case (NODE_FUNCSYNOPSISINFO):  
                 case (NODE_PARA):  
                 case (NODE_PROGRAMLISTING):  
                 case (NODE_REFDESCRIPTOR):  
                 case (NODE_REFENTRYTITLE):  
                 case (NODE_REFNAME):  
                 case (NODE_REFPURPOSE):  
                 case (NODE_SYNOPSIS):  
                 case (NODE_TITLE):  
                         return(1);  
                 default:  
                         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;  
         }  
   
         abort();  
         return(0);  
 }  
   
 /*  
  * Process a stream of characters.   * Process a stream of characters.
  * We store text as nodes in and of themselves.   * We store text as nodes in and of themselves.
  * If a text node is already open, append to it.   * If a text node is already open, append to it.
Line 422  xml_char(void *arg, const XML_Char *p, int sz)
Line 261  xml_char(void *arg, const XML_Char *p, int sz)
          */           */
         if (NODE_TEXT != ps->node) {          if (NODE_TEXT != ps->node) {
                 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;
Line 437  xml_char(void *arg, const XML_Char *p, int sz)
Line 276  xml_char(void *arg, const XML_Char *p, int sz)
                 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(NULL != ps->root);
Line 444  xml_char(void *arg, const XML_Char *p, int sz)
Line 284  xml_char(void *arg, const XML_Char *p, int sz)
   
         /* 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 + (size_t)sz);                  ps->cur->bsz + (size_t)sz);
         if (NULL == ps->cur->b) {          if (NULL == ps->cur->b) {
                 perror(NULL);                  perror(NULL);
Line 452  xml_char(void *arg, const XML_Char *p, int sz)
Line 292  xml_char(void *arg, const XML_Char *p, int sz)
         }          }
         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 += (size_t)sz;
           ps->cur->real = ps->cur->b;
 }  }
   
 static void  static void
Line 460  pnode_trim(struct pnode *pn)
Line 301  pnode_trim(struct pnode *pn)
   
         assert(NODE_TEXT == pn->node);          assert(NODE_TEXT == pn->node);
         for ( ; pn->bsz > 0; pn->bsz--)          for ( ; pn->bsz > 0; pn->bsz--)
                 if ( ! isspace((int)pn->b[pn->bsz - 1]))                  if ( ! isspace((unsigned char)pn->b[pn->bsz - 1]))
                         break;                          break;
 }  }
   
Line 481  pnode_trim(struct pnode *pn)
Line 322  pnode_trim(struct pnode *pn)
 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;          enum nodeid       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 || 0 == strcmp(name, "xi:include"))
                 return;                  return;
   
         /* Close out text node, if applicable... */          /* Close out text node, if applicable... */
Line 503  xml_elem_start(void *arg, const XML_Char *name, const 
Line 349  xml_elem_start(void *arg, const XML_Char *name, const 
                 else if (0 == strcmp(nodes[node].name, name))                  else if (0 == strcmp(nodes[node].name, name))
                         break;                          break;
   
         /* FIXME: do more with these error messages... */  
         if (NODE__MAX == node && NODE_ROOT == ps->node) {          if (NODE__MAX == node && NODE_ROOT == ps->node) {
                 fprintf(stderr, "%s: ignoring node\n", name);  
                 return;                  return;
         } else if (NODE__MAX == node) {          } else if (NODE__MAX == node) {
                 fprintf(stderr, "%s: unknown node\n", name);                  fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n",
                           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 (NODE_ROOT == ps->node && NULL != ps->root) {
                 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) {          } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node &&
                 fprintf(stderr, "%s: known node w/o context\n", name);              NODE_PART != node && NODE_BOOK != node) {
                 return;                  return;
         } else if ( ! isparent(node, ps->node)) {  
                 fprintf(stderr, "%s: bad parent\n", name);  
                 ps->stop = 1;  
                 return;  
         }          }
   
           if (NODE_INLINEEQUATION == node)
                   ps->flags |= PARSE_EQN;
   
         if (NULL == (dat = calloc(1, sizeof(struct pnode)))) {          if (NULL == (dat = calloc(1, sizeof(struct pnode)))) {
                 perror(NULL);                  perror(NULL);
                 exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
Line 532  xml_elem_start(void *arg, const XML_Char *name, const 
Line 379  xml_elem_start(void *arg, const XML_Char *name, const 
         dat->node = ps->node = node;          dat->node = ps->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 (NULL != ps->cur)
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);                  TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
Line 539  xml_elem_start(void *arg, const XML_Char *name, const 
Line 387  xml_elem_start(void *arg, const XML_Char *name, const 
         ps->cur = dat;          ps->cur = dat;
         if (NULL == ps->root)          if (NULL == ps->root)
                 ps->root = dat;                  ps->root = dat;
   
           /*
            * Process attributes.
            */
           for (att = atts; NULL != *att; att += 2) {
                   for (key = 0; key < ATTRKEY__MAX; key++)
                           if (0 == strcmp(*att, attrkeys[key]))
                                   break;
                   if (ATTRKEY__MAX == key) {
                           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 (0 == strcmp(*(att + 1), attrvals[val]))
                                   break;
                   pattr = calloc(1, sizeof(struct pattr));
                   pattr->key = key;
                   pattr->val = val;
                   if (ATTRVAL__MAX == val)
                           pattr->rawval = strdup(*(att + 1));
                   TAILQ_INSERT_TAIL(&dat->attrq, pattr, child);
           }
   
 }  }
   
 /*  /*
Line 551  xml_elem_end(void *arg, const XML_Char *name)
Line 428  xml_elem_end(void *arg, const XML_Char *name)
 {  {
         struct parse    *ps = arg;          struct parse    *ps = arg;
   
           /* FIXME: find a better way to ditch other namespaces. */
         if (ps->stop || NODE_ROOT == ps->node)          if (ps->stop || NODE_ROOT == ps->node)
                 return;                  return;
           else if (0 == strcmp(name, "xi:include"))
                   return;
   
         /* Close out text node, if applicable... */          /* Close out text node, if applicable... */
         if (NODE_TEXT == ps->node) {          if (NODE_TEXT == ps->node) {
Line 576  static void
Line 456  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 (NULL == pn)
                 return;                  return;
Line 585  pnode_free(struct pnode *pn)
Line 466  pnode_free(struct pnode *pn)
                 pnode_free(pp);                  pnode_free(pp);
         }          }
   
         free(pn->b);          while (NULL != (ap = TAILQ_FIRST(&pn->attrq))) {
                   TAILQ_REMOVE(&pn->attrq, ap, child);
                   free(ap->rawval);
                   free(ap);
           }
   
           free(pn->real);
         free(pn);          free(pn);
 }  }
   
Line 625  bufclear(struct parse *p)
Line 512  bufclear(struct parse *p)
 /*  /*
  * Append NODE_TEXT contents to the current buffer, reallocating its   * Append NODE_TEXT contents to the current buffer, reallocating its
  * size if necessary.   * size if necessary.
  * The buffer is ALWAYS nil-terminated.   * The buffer is ALWAYS NUL-terminated.
  */   */
 static void  static void
 bufappend(struct parse *p, struct pnode *pn)  bufappend(struct parse *p, struct pnode *pn)
Line 662  bufappend_r(struct parse *p, struct pnode *pn)
Line 549  bufappend_r(struct parse *p, struct pnode *pn)
 }  }
   
 /*  /*
    * Recursively search and return the first instance of "node".
    */
   static struct pnode *
   pnode_findfirst(struct pnode *pn, enum nodeid node)
   {
           struct pnode    *pp, *res;
   
           res = NULL;
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   res = pp->node == node ? pp :
                           pnode_findfirst(pp, node);
                   if (NULL != res)
                           break;
           }
   
           return(res);
   }
   
   #define MACROLINE_NORM  0
   #define MACROLINE_UPPER 1
   #define MACROLINE_NOWS 2
   /*
  * Recursively print text presumably on a macro line.   * Recursively print text presumably on a macro line.
  * Convert all whitespace to regular spaces.   * Convert all whitespace to regular spaces.
  */   */
 static void  static void
 pnode_printmacrolinepart(struct parse *p, struct pnode *pn)  pnode_printmacrolinetext(struct parse *p, struct pnode *pn, int fl)
 {  {
         char            *cp;          char            *cp;
   
           if (0 == p->newln && ! (MACROLINE_NOWS & fl))
                   putchar(' ');
   
         bufclear(p);          bufclear(p);
         bufappend_r(p, pn);          bufappend_r(p, pn);
   
         /* Convert all space to spaces. */          /* Convert all space to spaces. */
         for (cp = p->b; '\0' != *cp; cp++)          for (cp = p->b; '\0' != *cp; cp++)
                 if (isspace((int)*cp))                  if (isspace((unsigned char)*cp))
                         *cp = ' ';                          *cp = ' ';
   
         for (cp = p->b; isspace((int)*cp); cp++)          for (cp = p->b; isspace((unsigned char)*cp); cp++)
                 /* Spin past whitespace (XXX: necessary?) */ ;                  /* Spin past whitespace (XXX: necessary?) */ ;
         for ( ; '\0' != *cp; cp++) {          for ( ; '\0' != *cp; cp++) {
                 /* Escape us if we look like a macro. */                  /* Escape us if we look like a macro. */
                 if ((cp == p->b || ' ' == *(cp - 1)) &&                  if ((cp == p->b || ' ' == *(cp - 1)) &&
                         isupper((int)*cp) &&                          isupper((unsigned char)*cp) &&
                         '\0' != *(cp + 1) &&                          '\0' != *(cp + 1) &&
                         islower((int)*(cp + 1)) &&                          islower((unsigned char)*(cp + 1)) &&
                         ('\0' == *(cp + 2) ||                          ('\0' == *(cp + 2) ||
                          ' ' == *(cp + 2) ||                           ' ' == *(cp + 2) ||
                          (islower((int)*(cp + 2)) &&                           (islower((unsigned char)*(cp + 2)) &&
                           ('\0' == *(cp + 3) ||                            ('\0' == *(cp + 3) ||
                            ' ' == *(cp + 3)))))                             ' ' == *(cp + 3)))))
                         fputs("\\&", stdout);                          fputs("\\&", stdout);
                 putchar(*cp);                  if (MACROLINE_UPPER & fl)
                           putchar(toupper((unsigned char)*cp));
                   else
                           putchar(*cp);
                 /* If we're a character escape, escape us. */                  /* If we're a character escape, escape us. */
                 if ('\\' == *cp)                  if ('\\' == *cp)
                         putchar('e');                          putchar('e');
         }          }
 }  }
   
   static void
   pnode_printmacrolinepart(struct parse *p, struct pnode *pn)
   {
   
           pnode_printmacrolinetext(p, pn, 0);
   }
   
 /*  /*
  * Just pnode_printmacrolinepart() but with a newline.   * Just pnode_printmacrolinepart() but with a newline.
  * If no text, just the newline.   * If no text, just the newline.
Line 707  static void
Line 629  static void
 pnode_printmacroline(struct parse *p, struct pnode *pn)  pnode_printmacroline(struct parse *p, struct pnode *pn)
 {  {
   
         pnode_printmacrolinepart(p, pn);          assert(0 == p->newln);
           pnode_printmacrolinetext(p, pn, 0);
         putchar('\n');          putchar('\n');
           p->newln = 1;
 }  }
   
 static void  static void
Line 732  pnode_printmclose(struct parse *p, int sv)
Line 656  pnode_printmclose(struct parse *p, int sv)
 }  }
   
 /*  /*
    * Like pnode_printmclose() except we look to the next node, and, if
    * found, see if it starts with punctuation.
    * If it does, then we print that punctuation before the newline.
    */
   static void
   pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv)
   {
           /* We wouldn't have done anything anyway. */
           if ( ! (sv && ! p->newln))
                   return;
   
           /* No next node or it's not text. */
           if (NULL == (pn = TAILQ_NEXT(pn, child))) {
                   pnode_printmclose(p, sv);
                   return;
           } else if (NODE_TEXT != pn->node) {
                   pnode_printmclose(p, sv);
                   return;
           }
   
           /* Only do this for the comma/period. */
           if (pn->bsz > 0 &&
                   (',' == pn->b[0] || '.' == pn->b[0]) &&
                   (1 == pn->bsz || isspace((unsigned char)pn->b[1]))) {
                   putchar(' ');
                   putchar(pn->b[0]);
                   pn->b++;
                   pn->bsz--;
           }
   
           putchar('\n');
           p->newln = 1;
   }
   
   /*
  * If the SYNOPSIS macro has a superfluous title, kill it.   * If the SYNOPSIS macro has a superfluous title, kill it.
  */   */
 static void  static void
Line 739  pnode_printrefsynopsisdiv(struct parse *p, struct pnod
Line 698  pnode_printrefsynopsisdiv(struct parse *p, struct pnod
 {  {
         struct pnode    *pp;          struct pnode    *pp;
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TITLE == pp->node) {                  if (NODE_TITLE == pp->node) {
                         pnode_unlink(pp);                          pnode_unlink(pp);
                         return;                          return;
Line 753  static void
Line 712  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;
   
           level = ++p->level;
           flags = 1 == level ? MACROLINE_UPPER : 0;
           if (3 > level) {
                   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 (NODE_TITLE == pp->node)
                         break;                          break;
   
         fputs(".Sh ", stdout);          if (NULL == pp) {
                   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;
                   }
           }
   
           switch (level) {
           case (1):
                   fputs(".Sh", stdout);
                   break;
           case (2):
                   fputs(".Ss", stdout);
                   break;
           default:
                   puts(".Pp");
                   fputs(".Sy", stdout);
                   break;
           }
   
         if (NULL != pp) {          if (NULL != pp) {
                 pnode_printmacroline(p, pp);                  p->newln = 0;
                   pnode_printmacrolinetext(p, pp, flags);
                   pnode_printmclose(p, 1);
                 pnode_unlink(pp);                  pnode_unlink(pp);
         } else          } else
                 puts("UNKNOWN");                  printf(" %s\n", title);
 }  }
   
 /*  /*
Line 782  pnode_printciterefentry(struct parse *p, struct pnode 
Line 794  pnode_printciterefentry(struct parse *p, struct pnode 
                 else if (NODE_REFENTRYTITLE == pp->node)                  else if (NODE_REFENTRYTITLE == pp->node)
                         title = pp;                          title = pp;
   
         fputs(".Xr ", stdout);  
   
         if (NULL != title) {          if (NULL != title) {
                 pnode_printmacrolinepart(p, title);                  pnode_printmacrolinepart(p, title);
                 putchar(' ');  
         } else          } else
                 fputs("unknown ", stdout);                  fputs(" unknown ", stdout);
   
         if (NULL != manvol)          if (NULL == manvol) {
                 pnode_printmacroline(p, manvol);                  puts(" 1");
         else                  p->newln = 1;
                 puts("1");          } else
                   pnode_printmacrolinepart(p, manvol);
 }  }
   
 static void  static void
Line 802  pnode_printrefmeta(struct parse *p, struct pnode *pn)
Line 812  pnode_printrefmeta(struct parse *p, struct pnode *pn)
         struct pnode    *pp, *title, *manvol;          struct pnode    *pp, *title, *manvol;
   
         title = manvol = NULL;          title = manvol = NULL;
           assert(p->newln);
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_MANVOLNUM == pp->node)                  if (NODE_MANVOLNUM == pp->node)
                         manvol = pp;                          manvol = pp;
                 else if (NODE_REFENTRYTITLE == pp->node)                  else if (NODE_REFENTRYTITLE == pp->node)
                         title = pp;                          title = pp;
   
         puts(".Dd $Mdocdate" "$");          fputs(".Dt", stdout);
         fputs(".Dt ", stdout);          p->newln = 0;
   
         if (NULL != title) {          if (NULL != title)
                 /* FIXME: uppercase. */                  pnode_printmacrolinetext(p, title, MACROLINE_UPPER);
                 pnode_printmacrolinepart(p, title);  
                 putchar(' ');  
         } else  
                 fputs("UNKNOWN ", stdout);  
   
         if (NULL != manvol)  
                 pnode_printmacroline(p, manvol);  
         else          else
                 puts("1");                  fputs(" UNKNOWN ", stdout);
   
         puts(".Os");          if (NULL == manvol) {
                   puts(" 1");
                   p->newln = 1;
           } else
                   pnode_printmacroline(p, manvol);
 }  }
   
 static void  static void
Line 831  pnode_printfuncdef(struct parse *p, struct pnode *pn)
Line 839  pnode_printfuncdef(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *ftype, *func;          struct pnode    *pp, *ftype, *func;
   
           assert(p->newln);
         ftype = func = NULL;          ftype = func = NULL;
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TEXT == pp->node)                  if (NODE_TEXT == pp->node)
Line 839  pnode_printfuncdef(struct parse *p, struct pnode *pn)
Line 848  pnode_printfuncdef(struct parse *p, struct pnode *pn)
                         func = pp;                          func = pp;
   
         if (NULL != ftype) {          if (NULL != ftype) {
                 fputs(".Ft ", stdout);                  fputs(".Ft", stdout);
                   p->newln = 0;
                 pnode_printmacroline(p, ftype);                  pnode_printmacroline(p, ftype);
         }          }
   
         if (NULL != func) {          if (NULL != func) {
                 fputs(".Fo ", stdout);                  fputs(".Fo", stdout);
                   p->newln = 0;
                 pnode_printmacroline(p, func);                  pnode_printmacroline(p, func);
         } else          } else {
                 puts(".Fo UNKNOWN");                  puts(".Fo UNKNOWN");
                   p->newln = 1;
           }
 }  }
   
 static void  static void
Line 855  pnode_printparamdef(struct parse *p, struct pnode *pn)
Line 868  pnode_printparamdef(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *ptype, *param;          struct pnode    *pp, *ptype, *param;
   
           assert(p->newln);
         ptype = param = NULL;          ptype = param = NULL;
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TEXT == pp->node)                  if (NODE_TEXT == pp->node)
Line 863  pnode_printparamdef(struct parse *p, struct pnode *pn)
Line 877  pnode_printparamdef(struct parse *p, struct pnode *pn)
                         param = pp;                          param = pp;
   
         fputs(".Fa \"", stdout);          fputs(".Fa \"", stdout);
           p->newln = 0;
         if (NULL != ptype) {          if (NULL != ptype) {
                 pnode_printmacrolinepart(p, ptype);                  pnode_printmacrolinetext(p, ptype, MACROLINE_NOWS);
                 putchar(' ');                  putchar(' ');
         }          }
   
Line 872  pnode_printparamdef(struct parse *p, struct pnode *pn)
Line 887  pnode_printparamdef(struct parse *p, struct pnode *pn)
                 pnode_printmacrolinepart(p, param);                  pnode_printmacrolinepart(p, param);
   
         puts("\"");          puts("\"");
           p->newln = 1;
 }  }
   
   /*
    * 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_printmathfenced(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
           struct pattr    *ap;
   
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ATTRKEY_OPEN == ap->key) {
                           printf("left %s ", ap->rawval);
                           break;
                   }
           if (NULL == ap)
                   printf("left ( ");
   
           pp = TAILQ_FIRST(&pn->childq);
           pnode_print(p, pp);
   
           while (NULL != (pp = TAILQ_NEXT(pp, child))) {
                   putchar(',');
                   pnode_print(p, pp);
           }
   
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ATTRKEY_CLOSE == ap->key) {
                           printf("right %s ", ap->rawval);
                           break;
                   }
           if (NULL == ap)
                   printf("right ) ");
   }
   
   /*
    * These math nodes require special handling because they have infix
    * syntax, instead of the usual prefix or prefix.
    * So we need to break up the first and second child node with a
    * particular eqn(7) word.
    */
   static void
   pnode_printmath(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           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);
   }
   
   static void
 pnode_printfuncprototype(struct parse *p, struct pnode *pn)  pnode_printfuncprototype(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp, *fdef;          struct pnode    *pp, *fdef;
   
           assert(p->newln);
         TAILQ_FOREACH(fdef, &pn->childq, child)          TAILQ_FOREACH(fdef, &pn->childq, child)
                 if (NODE_FUNCDEF == fdef->node)                  if (NODE_FUNCDEF == fdef->node)
                         break;                          break;
   
         if (NULL != fdef)          if (NULL != fdef)
Line 888  pnode_printfuncprototype(struct parse *p, struct pnode
Line 974  pnode_printfuncprototype(struct parse *p, struct pnode
         else          else
                 puts(".Fo UNKNOWN");                  puts(".Fo UNKNOWN");
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_PARAMDEF == pp->node)                  if (NODE_PARAMDEF == pp->node)
                         pnode_printparamdef(p, pp);                          pnode_printparamdef(p, pp);
   
         puts(".Fc");          puts(".Fc");
           p->newln = 1;
 }  }
   
 /*  /*
  * The <arg> element is more complicated than it should be because text   * 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   * nodes are treated like ".Ar foo", but non-text nodes need to be
  * re-sent into the printer (i.e., without the preceding ".Ar").   * re-sent into the printer (i.e., without the preceding ".Ar").
  * TODO: handle "optional" attribute.   * This also handles the case of "repetition" (or in other words, the
    * ellipsis following an argument) and optionality.
  */   */
 static void  static void
 pnode_printarg(struct parse *p, struct pnode *pn)  pnode_printarg(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           struct pattr    *ap;
           int              isop, isrep;
   
           isop = 1;
           isrep = 0;
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ATTRKEY_CHOICE == ap->key &&
                           (ATTRVAL_PLAIN == ap->val ||
                            ATTRVAL_REQ == ap->val))
                           isop = 0;
                   else if (ATTRKEY_REP == ap->key &&
                           (ATTRVAL_REPEAT == ap->val))
                           isrep = 1;
   
           if (isop) {
                   pnode_printmopen(p);
                   fputs("Op", stdout);
           }
   
         TAILQ_FOREACH(pp, &pn->childq, child) {          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_TEXT == pp->node) {                  if (NODE_TEXT == pp->node) {
                         pnode_printmopen(p);                          pnode_printmopen(p);
                         fputs("Ar ", stdout);                          fputs("Ar", stdout);
                 }                  }
                 pnode_print(p, pp);                  pnode_print(p, pp);
                   if (NODE_TEXT == pp->node && isrep)
                           fputs("...", stdout);
         }          }
 }  }
   
 /*  static void
  * Recursively search and return the first instance of "node".  pnode_printgroup(struct parse *p, struct pnode *pn)
  */  
 static struct pnode *  
 pnode_findfirst(struct pnode *pn, enum nodeid node)  
 {  {
         struct pnode    *pp, *res;          struct pnode    *pp, *np;
           struct pattr    *ap;
           int              isop, sv;
   
         res = NULL;          isop = 1;
         TAILQ_FOREACH(pp, &pn->childq, child) {          TAILQ_FOREACH(ap, &pn->attrq, child)
                 res = pp->node == node ? pp :                  if (ATTRKEY_CHOICE == ap->key &&
                         pnode_findfirst(pp, node);                          (ATTRVAL_PLAIN == ap->val ||
                 if (NULL != res)                           ATTRVAL_REQ == ap->val)) {
                           isop = 0;
                         break;                          break;
                   }
   
           /*
            * Make sure we're on a macro line.
            * This will prevent pnode_print() for putting us on a
            * subsequent line.
            */
           sv = p->newln;
           pnode_printmopen(p);
           if (isop)
                   fputs("Op", stdout);
           else if (sv)
                   fputs("No", stdout);
   
           /*
            * 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 (NULL != np) {
                           if (pp->node != np->node)
                                   break;
                           fputs(" |", stdout);
                           pnode_printmacrolinepart(p, np);
                           pp = np;
                           np = TAILQ_NEXT(np, child);
                   }
         }          }
   
         return(res);          pnode_printmclose(p, sv);
 }  }
   
 static void  static void
 pnode_printprologue(struct parse *p, struct pnode *pn)  pnode_printprologue(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           struct pattr    *ap;
           const char      *name;
   
         pp = NULL == p->root ? NULL :          pp = NULL == p->root ? NULL :
                 pnode_findfirst(p->root, NODE_REFMETA);                  pnode_findfirst(p->root, NODE_REFMETA);
   
           puts(".Dd $Mdocdate" "$");
         if (NULL != pp) {          if (NULL != pp) {
                 pnode_printrefmeta(p, pp);                  pnode_printrefmeta(p, pp);
                 pnode_unlink(pp);                  pnode_unlink(pp);
         } else {          } else {
                 puts(".\\\" Supplying bogus prologue...");                  name = "UNKNOWN";
                 puts(".Dd $Mdocdate" "$");                  TAILQ_FOREACH(ap, &p->root->attrq, child) {
                 puts(".Dt UNKNOWN 1");                          if (ATTRKEY_ID == ap->key) {
                 puts(".Os");                                  name = ap->rawval;
                                   break;
                           }
                   }
                   printf(".Dt %s 1\n", name);
         }          }
           puts(".Os");
   
           if (PARSE_EQN & p->flags) {
                   puts(".EQ");
                   puts("delim $$");
                   puts(".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;
   
           assert(p->newln);
           fputs(".It", stdout);
           p->newln = 0;
   
           TAILQ_FOREACH(pp, &pn->childq, child)
                   if (NODE_TERM == pp->node) {
                           if ( ! first)
                                   putchar(',');
                           pnode_print(p, pp);
                           pnode_unlink(pp);
                           first = 0;
                   } else
                           break;
   
           putchar('\n');
           p->newln = 1;
   }
   
   static void
   pnode_printrow(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           puts(".Bl -dash -compact");
   
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   assert(p->newln);
                   puts(".It");
                   pnode_print(p, pp);
                   pnode_printmclose(p, 1);
           }
           assert(p->newln);
           puts(".El");
   }
   
   static void
   pnode_printtable(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           assert(p->newln);
           TAILQ_FOREACH(pp, &pn->childq, child)
                   if (NODE_TITLE == pp->node) {
                           puts(".Pp");
                           pnode_print(p, pp);
                           pnode_unlink(pp);
                   }
           assert(p->newln);
           puts(".Bl -ohang");
           while (NULL != (pp = pnode_findfirst(pn, NODE_ROW))) {
                   puts(".It Table Row");
                   pnode_printrow(p, pp);
                   pnode_printmclose(p, 1);
                   pnode_unlink(pp);
           }
           assert(p->newln);
           puts(".El");
   }
   
   static void
   pnode_printlist(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           assert(p->newln);
           TAILQ_FOREACH(pp, &pn->childq, child)
                   if (NODE_TITLE == pp->node) {
                           puts(".Pp");
                           pnode_print(p, pp);
                           pnode_unlink(pp);
                   }
           assert(p->newln);
   
           if (NODE_ORDEREDLIST == pn->node)
                   puts(".Bl -enum");
           else
                   puts(".Bl -item");
   
           TAILQ_FOREACH(pp, &pn->childq, child) {
                   assert(p->newln);
                   puts(".It");
                   pnode_print(p, pp);
                   pnode_printmclose(p, 1);
           }
           assert(p->newln);
           puts(".El");
   }
   
   static void
   pnode_printvariablelist(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           assert(p->newln);
           TAILQ_FOREACH(pp, &pn->childq, child)
                   if (NODE_TITLE == pp->node) {
                           puts(".Pp");
                           pnode_print(p, pp);
                           pnode_unlink(pp);
                   }
   
           assert(p->newln);
           puts(".Bl -tag -width Ds");
           TAILQ_FOREACH(pp, &pn->childq, child)
                   if (NODE_VARLISTENTRY != pp->node) {
                           assert(p->newln);
                           fputs(".It", stdout);
                           pnode_printmacroline(p, pp);
                   } else {
                           assert(p->newln);
                           pnode_print(p, pp);
                   }
           assert(p->newln);
           puts(".El");
   }
   
   /*
  * 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)
Line 970  pnode_print(struct parse *p, struct pnode *pn)
Line 1252  pnode_print(struct parse *p, struct pnode *pn)
   
         sv = p->newln;          sv = p->newln;
   
           /* XXX fprintf(stderr, "NODE %s\n", nodes[pn->node].name); */
         switch (pn->node) {          switch (pn->node) {
           case (NODE_APPLICATION):
                   pnode_printmopen(p);
                   fputs("Nm", stdout);
                   break;
           case (NODE_ANCHOR):
                   /* Don't print anything! */
                   return;
         case (NODE_ARG):          case (NODE_ARG):
                 pnode_printarg(p, pn);                  pnode_printarg(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_CITEREFENTRY):          case (NODE_AUTHOR):
                   pnode_printmopen(p);
                   fputs("An", stdout);
                   break;
           case (NODE_AUTHORGROUP):
                 assert(p->newln);                  assert(p->newln);
                   puts(".An -split");
                   break;
           case (NODE_BOOKINFO):
                   assert(p->newln);
                   puts(".Sh NAME");
                   break;
           case (NODE_CITEREFENTRY):
                   pnode_printmopen(p);
                   fputs("Xr", stdout);
                 pnode_printciterefentry(p, pn);                  pnode_printciterefentry(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_CODE):          case (NODE_CODE):
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Li ", stdout);                  fputs("Li", stdout);
                 break;                  break;
         case (NODE_COMMAND):          case (NODE_COMMAND):
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Nm ", stdout);                  fputs("Nm", stdout);
                 break;                  break;
           case (NODE_CONSTANT):
                   pnode_printmopen(p);
                   fputs("Dv", stdout);
                   break;
           case (NODE_EDITOR):
                   puts("editor: ");
                   pnode_printmopen(p);
                   fputs("An", stdout);
                   break;
           case (NODE_EMPHASIS):
                   pnode_printmopen(p);
                   fputs("Em", stdout);
                   break;
           case (NODE_ENVAR):
                   pnode_printmopen(p);
                   fputs("Ev", stdout);
                   break;
           case (NODE_FILENAME):
                   pnode_printmopen(p);
                   fputs("Pa", stdout);
                   break;
         case (NODE_FUNCTION):          case (NODE_FUNCTION):
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Fn ", stdout);                  fputs("Fn", stdout);
                 break;                  break;
         case (NODE_FUNCPROTOTYPE):          case (NODE_FUNCPROTOTYPE):
                 assert(p->newln);                  assert(p->newln);
Line 999  pnode_print(struct parse *p, struct pnode *pn)
Line 1323  pnode_print(struct parse *p, struct pnode *pn)
                 break;                  break;
         case (NODE_FUNCSYNOPSISINFO):          case (NODE_FUNCSYNOPSISINFO):
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Fd ", stdout);                  fputs("Fd", stdout);
                 break;                  break;
           case (NODE_INFORMALEQUATION):
                   if ( ! p->newln)
                           putchar('\n');
                   puts(".EQ");
                   p->newln = 0;
                   break;
           case (NODE_INLINEEQUATION):
                   fputc('$', stdout);
                   p->newln = 0;
                   break;
           case (NODE_ITEMIZEDLIST):
                   assert(p->newln);
                   pnode_printlist(p, pn);
                   pnode_unlinksub(pn);
                   break;
           case (NODE_GROUP):
                   pnode_printgroup(p, pn);
                   pnode_unlinksub(pn);
                   break;
           case (NODE_LEGALNOTICE):
                   assert(p->newln);
                   puts(".Sh LEGAL NOTICE");
                   break;
           case (NODE_LITERAL):
                   pnode_printmopen(p);
                   fputs("Li", stdout);
                   break;
           case (NODE_MML_MFENCED):
                   pnode_printmathfenced(p, pn);
                   pnode_unlinksub(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);
                   pnode_unlinksub(pn);
                   break;
         case (NODE_OPTION):          case (NODE_OPTION):
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Fl ", stdout);                  fputs("Fl", stdout);
                 break;                  break;
           case (NODE_ORDEREDLIST):
                   assert(p->newln);
                   pnode_printlist(p, pn);
                   pnode_unlinksub(pn);
                   break;
         case (NODE_PARA):          case (NODE_PARA):
                 assert(p->newln);                  assert(p->newln);
                   if (NULL != pn->parent &&
                           NODE_LISTITEM == pn->parent->node)
                           break;
                 puts(".Pp");                  puts(".Pp");
                 break;                  break;
         case (NODE_PARAMETER):          case (NODE_PARAMETER):
                 /* Suppress non-text children... */                  /* Suppress non-text children... */
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Fa \"", stdout);                  fputs("Fa \"", stdout);
                 pnode_printmacrolinepart(p, pn);                  pnode_printmacrolinetext(p, pn, MACROLINE_NOWS);
                 puts("\"");                  fputs("\"", stdout);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
           case (NODE_QUOTE):
                   pnode_printmopen(p);
                   fputs("Qo", stdout);
                   break;
           case (NODE_LITERALLAYOUT):
                   /* FALLTHROUGH */
         case (NODE_PROGRAMLISTING):          case (NODE_PROGRAMLISTING):
                   /* FALLTHROUGH */
           case (NODE_SCREEN):
                 assert(p->newln);                  assert(p->newln);
                 puts(".Bd -literal");                  puts(".Bd -literal");
                 break;                  break;
           case (NODE_REFENTRYINFO):
                   /* Suppress. */
                   pnode_unlinksub(pn);
                   break;
         case (NODE_REFMETA):          case (NODE_REFMETA):
                 abort();                  abort();
                 break;                  break;
         case (NODE_REFNAME):          case (NODE_REFNAME):
                 /* Suppress non-text children... */                  /* Suppress non-text children... */
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Nm ", stdout);                  fputs("Nm", stdout);
                   p->newln = 0;
                 pnode_printmacrolinepart(p, pn);                  pnode_printmacrolinepart(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
Line 1037  pnode_print(struct parse *p, struct pnode *pn)
Line 1427  pnode_print(struct parse *p, struct pnode *pn)
                 break;                  break;
         case (NODE_REFPURPOSE):          case (NODE_REFPURPOSE):
                 assert(p->newln);                  assert(p->newln);
                 fputs(".Nd ", stdout);                  pnode_printmopen(p);
                   fputs("Nd", stdout);
                 break;                  break;
         case (NODE_REFSYNOPSISDIV):          case (NODE_REFSYNOPSISDIV):
                 assert(p->newln);                  assert(p->newln);
                 pnode_printrefsynopsisdiv(p, pn);                  pnode_printrefsynopsisdiv(p, pn);
                 puts(".Sh SYNOPSIS");                  puts(".Sh SYNOPSIS");
                 break;                  break;
           case (NODE_PREFACE):
         case (NODE_REFSECT1):          case (NODE_REFSECT1):
           case (NODE_REFSECT2):
           case (NODE_REFSECT3):
           case (NODE_REFSECTION):
           case (NODE_CHAPTER):
           case (NODE_SECT1):
           case (NODE_SECT2):
           case (NODE_SECTION):
           case (NODE_NOTE):
           case (NODE_TIP):
           case (NODE_CAUTION):
           case (NODE_WARNING):
                 assert(p->newln);                  assert(p->newln);
                 pnode_printrefsect(p, pn);                  pnode_printrefsect(p, pn);
                 break;                  break;
           case (NODE_REPLACEABLE):
                   pnode_printmopen(p);
                   fputs("Ar", stdout);
                   break;
           case (NODE_SBR):
                   assert(p->newln);
                   puts(".br");
                   break;
           case (NODE_SGMLTAG):
                   pnode_printmopen(p);
                   fputs("Li", stdout);
                   break;
         case (NODE_STRUCTNAME):          case (NODE_STRUCTNAME):
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Vt ", stdout);                  fputs("Vt", stdout);
                 break;                  break;
           case (NODE_TABLE):
                   /* FALLTHROUGH */
           case (NODE_INFORMALTABLE):
                   assert(p->newln);
                   pnode_printtable(p, pn);
                   pnode_unlinksub(pn);
                   break;
         case (NODE_TEXT):          case (NODE_TEXT):
                   if (0 == p->newln)
                           putchar(' ');
   
                 bufclear(p);                  bufclear(p);
                 bufappend(p, pn);                  bufappend(p, pn);
   
                   if (0 == p->bsz) {
                           assert(pn->real != pn->b);
                           break;
                   }
   
                 /*                  /*
                  * Output all characters, squeezing out whitespace                   * Output all characters, squeezing out whitespace
                  * between newlines.                   * between newlines.
                  * XXX: all whitespace, including tabs (?).                   * XXX: all whitespace, including tabs (?).
                  * Remember to escape control characters and escapes.                   * Remember to escape control characters and escapes.
                  */                   */
                 assert(p->bsz);                  assert(p->bsz);
                 for (last = '\n', cp = p->b; '\0' != *cp; ) {                  cp = p->b;
   
                   /*
                    * There's often a superfluous "-" in its <option> tags
                    * before the actual flags themselves.
                    * "Fl" does this for us, so remove it.
                    */
                   if (NULL != pn->parent &&
                           NODE_OPTION == pn->parent->node &&
                           '-' == *cp)
                           cp++;
                   for (last = '\n'; '\0' != *cp; ) {
                         if ('\n' == last) {                          if ('\n' == last) {
                                 /* 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)
Line 1079  pnode_print(struct parse *p, struct pnode *pn)
Line 1521  pnode_print(struct parse *p, struct pnode *pn)
                 }                  }
                 p->newln = 0;                  p->newln = 0;
                 break;                  break;
           case (NODE_TITLE):
                   if (pn->parent->node == NODE_BOOKINFO) {
                           pnode_printmopen(p);
                           fputs("Nd", stdout);
                   }
                   break;
           case (NODE_TYPE):
                   pnode_printmopen(p);
                   fputs("Vt", stdout);
                   break;
           case (NODE_USERINPUT):
                   pnode_printmopen(p);
                   fputs("Li", stdout);
                   break;
           case (NODE_VARIABLELIST):
                   assert(p->newln);
                   pnode_printvariablelist(p, pn);
                   pnode_unlinksub(pn);
                   break;
           case (NODE_VARLISTENTRY):
                   assert(p->newln);
                   pnode_printvarlistentry(p, pn);
                   break;
           case (NODE_VARNAME):
                   pnode_printmopen(p);
                   fputs("Va", stdout);
                   break;
         default:          default:
                 break;                  break;
         }          }
Line 1087  pnode_print(struct parse *p, struct pnode *pn)
Line 1556  pnode_print(struct parse *p, struct pnode *pn)
                 pnode_print(p, pp);                  pnode_print(p, pp);
   
         switch (pn->node) {          switch (pn->node) {
           case (NODE_INFORMALEQUATION):
                   if ( ! p->newln)
                           putchar('\n');
                   puts(".EN");
                   p->newln = 1;
                   break;
           case (NODE_INLINEEQUATION):
                   fputs("$ ", stdout);
                   p->newln = 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_ARG):
           case (NODE_AUTHOR):
           case (NODE_CITEREFENTRY):
         case (NODE_CODE):          case (NODE_CODE):
         case (NODE_COMMAND):          case (NODE_COMMAND):
           case (NODE_CONSTANT):
           case (NODE_EDITOR):
           case (NODE_EMPHASIS):
           case (NODE_ENVAR):
           case (NODE_FILENAME):
         case (NODE_FUNCTION):          case (NODE_FUNCTION):
         case (NODE_FUNCSYNOPSISINFO):          case (NODE_FUNCSYNOPSISINFO):
           case (NODE_LITERAL):
         case (NODE_OPTION):          case (NODE_OPTION):
         case (NODE_PARAMETER):          case (NODE_PARAMETER):
         case (NODE_REFNAME):          case (NODE_REPLACEABLE):
           case (NODE_REFPURPOSE):
           case (NODE_SGMLTAG):
         case (NODE_STRUCTNAME):          case (NODE_STRUCTNAME):
         case (NODE_TEXT):          case (NODE_TEXT):
           case (NODE_TYPE):
           case (NODE_USERINPUT):
           case (NODE_VARNAME):
                   pnode_printmclosepunct(p, pn, sv);
                   break;
           case (NODE_QUOTE):
                 pnode_printmclose(p, sv);                  pnode_printmclose(p, sv);
                   sv = p->newln;
                   pnode_printmopen(p);
                   fputs("Qc", stdout);
                   pnode_printmclose(p, sv);
                 break;                  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 (NULL != pn->parent &&
                           NODE_REFNAMEDIV == pn->parent->node &&
                           NULL != TAILQ_NEXT(pn, child) &&
                           NODE_REFNAME == TAILQ_NEXT(pn, child)->node)
                           fputs(" ,", stdout);
                   pnode_printmclose(p, sv);
                   break;
           case (NODE_PREFACE):
           case (NODE_REFSECT1):
           case (NODE_REFSECT2):
           case (NODE_REFSECT3):
           case (NODE_REFSECTION):
           case (NODE_CHAPTER):
           case (NODE_SECT1):
           case (NODE_SECT2):
           case (NODE_SECTION):
           case (NODE_NOTE):
           case (NODE_TIP):
           case (NODE_CAUTION):
           case (NODE_WARNING):
                   p->level--;
                   break;
           case (NODE_LITERALLAYOUT):
                   /* FALLTHROUGH */
         case (NODE_PROGRAMLISTING):          case (NODE_PROGRAMLISTING):
                   /* FALLTHROUGH */
           case (NODE_SCREEN):
                 assert(p->newln);                  assert(p->newln);
                 puts(".Ed");                  puts(".Ed");
                 p->newln = 1;                  p->newln = 1;
                 break;                  break;
           case (NODE_TITLE):
                   if (pn->parent->node == NODE_BOOKINFO) {
                           pnode_printmclose(p, 1);
                           puts(".Sh AUTHORS");
                   }
                   break;
         default:          default:
                 break;                  break;
         }          }
Line 1114  pnode_print(struct parse *p, struct pnode *pn)
Line 1660  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 1124  readfile(XML_Parser xp, int fd, 
Line 1670  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.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);
Line 1131  readfile(XML_Parser xp, int fd, 
Line 1679  readfile(XML_Parser xp, int fd, 
   
         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 (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz)))
                         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.
                  */                   */
Line 1161  main(int argc, char *argv[])
Line 1711  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 = 0;
   
         if (-1 != getopt(argc, argv, ""))          while (-1 != (ch = getopt(argc, argv, "W")))
                 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 = 0 == strcmp(fname, "-") ?
                 STDIN_FILENO : open(fname, O_RDONLY, 0);                  STDIN_FILENO : open(fname, O_RDONLY, 0);
   
         /*          /*
Line 1203  main(int argc, char *argv[])
Line 1767  main(int argc, char *argv[])
         if (STDIN_FILENO != fd)          if (STDIN_FILENO != fd)
                 close(fd);                  close(fd);
         return(rc ? EXIT_SUCCESS : EXIT_FAILURE);          return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
   
   usage:
           fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname);
           return(EXIT_FAILURE);
 }  }

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.52

CVSweb