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

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.28 and 1.62

version 1.28, 2014/04/02 10:27:29 version 1.62, 2019/03/22 17:42:53
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 37  struct parse {
Line 38  struct parse {
         enum nodeid      node; /* current (NODE_ROOT if pre-tree) */          enum nodeid      node; /* current (NODE_ROOT if pre-tree) */
         const char      *fname; /* filename */          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 */
 };  };
   
 struct  node {  struct  node {
         const char      *name; /* docbook element 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);
Line 64  struct pattr {
Line 67  struct pattr {
 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 */
Line 73  struct pnode {
Line 77  struct pnode {
   
 static  const char *attrkeys[ATTRKEY__MAX] = {  static  const char *attrkeys[ATTRKEY__MAX] = {
         "choice",          "choice",
           "class",
           "close",
         "id",          "id",
           "linkend",
           "open",
         "rep"          "rep"
 };  };
   
 static  const char *attrvals[ATTRVAL__MAX] = {  static  const char *attrvals[ATTRVAL__MAX] = {
           "monospaced",
         "norepeat",          "norepeat",
         "opt",          "opt",
         "plain",          "plain",
Line 85  static const char *attrvals[ATTRVAL__MAX] = {
Line 94  static const char *attrvals[ATTRVAL__MAX] = {
         "req"          "req"
 };  };
   
 static  const struct node nodes[NODE__MAX] = {  static  const struct node nodes[] = {
         { NULL, 0 },          { "acronym",            NODE_ACRONYM },
         { "acronym", 0 },          { "affiliation",        NODE_AFFILIATION },
         { "application", 0 },          { "anchor",             NODE_ANCHOR },
         { "arg", 0 },          { "application",        NODE_APPLICATION },
         { "citerefentry", NODE_IGNTEXT },          { "arg",                NODE_ARG },
         { "cmdsynopsis", NODE_IGNTEXT },          { "author",             NODE_AUTHOR },
         { "code", 0 },          { "authorgroup",        NODE_AUTHORGROUP },
         { "command", 0 },          { "blockquote",         NODE_BLOCKQUOTE },
         { "date", 0 },          { "book",               NODE_BOOK },
         { "emphasis", 0 },          { "bookinfo",           NODE_BOOKINFO },
         { "entry", 0 },          { "caution",            NODE_CAUTION },
         { "envar", 0 },          { "chapter",            NODE_SECTION },
         { "filename", 0 },          { "citerefentry",       NODE_CITEREFENTRY },
         { "funcdef", 0 },          { "citetitle",          NODE_CITETITLE },
         { "funcprototype", NODE_IGNTEXT },          { "cmdsynopsis",        NODE_CMDSYNOPSIS },
         { "funcsynopsis", NODE_IGNTEXT },          { "code",               NODE_CODE },
         { "funcsynopsisinfo", 0 },          { "colspec",            NODE_COLSPEC },
         { "function", 0 },          { "command",            NODE_COMMAND },
         { "itemizedlist", NODE_IGNTEXT },          { "constant",           NODE_CONSTANT },
         { "group", NODE_IGNTEXT },          { "copyright",          NODE_COPYRIGHT },
         { "link", 0 },          { "date",               NODE_DATE },
         { "listitem", NODE_IGNTEXT },          { "editor",             NODE_EDITOR },
         { "literal", 0 },          { "emphasis",           NODE_EMPHASIS },
         { "manvolnum", 0 },          { "entry",              NODE_ENTRY },
         { "option", 0 },          { "envar",              NODE_ENVAR },
         { "orderedlist", NODE_IGNTEXT },          { "fieldsynopsis",      NODE_FIELDSYNOPSIS },
         { "para", 0 },          { "filename",           NODE_FILENAME },
         { "paramdef", 0 },          { "firstname",          NODE_FIRSTNAME },
         { "parameter", 0 },          { "firstterm",          NODE_FIRSTTERM },
         { "programlisting", 0 },          { "footnote",           NODE_FOOTNOTE },
         { "prompt", 0 },          { "funcdef",            NODE_FUNCDEF },
         { "quote", 0 },          { "funcprototype",      NODE_FUNCPROTOTYPE },
         { "refclass", NODE_IGNTEXT },          { "funcsynopsis",       NODE_FUNCSYNOPSIS },
         { "refdescriptor", NODE_IGNTEXT },          { "funcsynopsisinfo",   NODE_FUNCSYNOPSISINFO },
         { "refentry", NODE_IGNTEXT },          { "function",           NODE_FUNCTION },
         { "refentryinfo", NODE_IGNTEXT },          { "glossterm",          NODE_GLOSSTERM },
         { "refentrytitle", 0 },          { "group",              NODE_GROUP },
         { "refmeta", NODE_IGNTEXT },          { "holder",             NODE_HOLDER },
         { "refmiscinfo", NODE_IGNTEXT },          { "index",              NODE_INDEX },
         { "refname", 0 },          { "indexterm",          NODE_INDEXTERM },
         { "refnamediv", NODE_IGNTEXT },          { "info",               NODE_INFO },
         { "refpurpose", 0 },          { "informalequation",   NODE_INFORMALEQUATION },
         { "refsect1", NODE_IGNTEXT },          { "informaltable",      NODE_INFORMALTABLE },
         { "refsect2", NODE_IGNTEXT },          { "inlineequation",     NODE_INLINEEQUATION },
         { "refsynopsisdiv", NODE_IGNTEXT },          { "itemizedlist",       NODE_ITEMIZEDLIST },
         { "replaceable", 0 },          { "keysym",             NODE_KEYSYM },
         { "row", NODE_IGNTEXT },          { "legalnotice",        NODE_LEGALNOTICE },
         { "sbr", NODE_IGNTEXT },          { "link",               NODE_LINK },
         { "screen", NODE_IGNTEXT },          { "listitem",           NODE_LISTITEM },
         { "structname", 0 },          { "literal",            NODE_LITERAL },
         { "synopsis", 0 },          { "literallayout",      NODE_LITERALLAYOUT },
         { "table", NODE_IGNTEXT },          { "manvolnum",          NODE_MANVOLNUM },
         { "tbody", NODE_IGNTEXT },          { "member",             NODE_MEMBER },
         { "term", 0 },          { "mml:math",           NODE_MML_MATH },
         { NULL, 0 },          { "mml:mfenced",        NODE_MML_MFENCED },
         { "tfoot", NODE_IGNTEXT },          { "mml:mfrac",          NODE_MML_MFRAC },
         { "tgroup", NODE_IGNTEXT },          { "mml:mi",             NODE_MML_MI },
         { "thead", NODE_IGNTEXT },          { "mml:mn",             NODE_MML_MN },
         { "title", 0 },          { "mml:mo",             NODE_MML_MO },
         { "ulink", 0 },          { "mml:mrow",           NODE_MML_MROW },
         { "userinput", 0 },          { "mml:msub",           NODE_MML_MSUB },
         { "variablelist", NODE_IGNTEXT },          { "mml:msup",           NODE_MML_MSUP },
         { "varlistentry", NODE_IGNTEXT },          { "modifier",           NODE_MODIFIER },
         { "varname", 0 },          { "note",               NODE_NOTE },
           { "option",             NODE_OPTION },
           { "orderedlist",        NODE_ORDEREDLIST },
           { "orgname",            NODE_ORGNAME },
           { "othername",          NODE_OTHERNAME },
           { "para",               NODE_PARA },
           { "paramdef",           NODE_PARAMDEF },
           { "parameter",          NODE_PARAMETER },
           { "part",               NODE_SECTION },
           { "phrase",             NODE_PHRASE },
           { "preface",            NODE_PREFACE },
           { "primary",            NODE_PRIMARY },
           { "programlisting",     NODE_PROGRAMLISTING },
           { "prompt",             NODE_PROMPT },
           { "quote",              NODE_QUOTE },
           { "refclass",           NODE_REFCLASS },
           { "refdescriptor",      NODE_REFDESCRIPTOR },
           { "refentry",           NODE_REFENTRY },
           { "refentryinfo",       NODE_REFENTRYINFO },
           { "refentrytitle",      NODE_REFENTRYTITLE },
           { "refmeta",            NODE_REFMETA },
           { "refmetainfo",        NODE_REFMETAINFO },
           { "refmiscinfo",        NODE_REFMISCINFO },
           { "refname",            NODE_REFNAME },
           { "refnamediv",         NODE_REFNAMEDIV },
           { "refpurpose",         NODE_REFPURPOSE },
           { "refsect1",           NODE_SECTION },
           { "refsect2",           NODE_SECTION },
           { "refsect3",           NODE_SECTION },
           { "refsection",         NODE_SECTION },
           { "refsynopsisdiv",     NODE_REFSYNOPSISDIV },
           { "releaseinfo",        NODE_RELEASEINFO },
           { "replaceable",        NODE_REPLACEABLE },
           { "row",                NODE_ROW },
           { "sbr",                NODE_SBR },
           { "screen",             NODE_SCREEN },
           { "secondary",          NODE_SECONDARY },
           { "sect1",              NODE_SECTION },
           { "sect2",              NODE_SECTION },
           { "section",            NODE_SECTION },
           { "sgmltag",            NODE_SGMLTAG },
           { "simplelist",         NODE_SIMPLELIST },
           { "spanspec",           NODE_SPANSPEC },
           { "structname",         NODE_STRUCTNAME },
           { "subtitle",           NODE_SUBTITLE },
           { "surname",            NODE_SURNAME },
           { "synopsis",           NODE_SYNOPSIS },
           { "table",              NODE_TABLE },
           { "tbody",              NODE_TBODY },
           { "term",               NODE_TERM },
           { "tfoot",              NODE_TFOOT },
           { "tgroup",             NODE_TGROUP },
           { "thead",              NODE_THEAD },
           { "tip",                NODE_TIP },
           { "title",              NODE_TITLE },
           { "trademark",          NODE_TRADEMARK },
           { "type",               NODE_TYPE },
           { "ulink",              NODE_ULINK },
           { "userinput",          NODE_USERINPUT },
           { "variablelist",       NODE_VARIABLELIST },
           { "varlistentry",       NODE_VARLISTENTRY },
           { "varname",            NODE_VARNAME },
           { "warning",            NODE_WARNING },
           { "wordasword",         NODE_WORDASWORD },
           { "year",               NODE_YEAR },
           { NULL,                 NODE__MAX }
 };  };
   
   static  int warn = 0;
   
 static void  static void
 pnode_print(struct parse *p, struct pnode *pn);  pnode_print(struct parse *p, struct pnode *pn);
   
Line 172  xml_char(void *arg, const XML_Char *p, int sz)
Line 248  xml_char(void *arg, const XML_Char *p, int sz)
         if (ps->stop || NODE_ROOT == ps->node)          if (ps->stop || NODE_ROOT == ps->node)
                 return;                  return;
   
         /* Not supposed to be collecting text. */  
         assert(NULL != ps->cur);          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?
Line 186  xml_char(void *arg, const XML_Char *p, int sz)
Line 259  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 209  xml_char(void *arg, const XML_Char *p, int sz)
Line 282  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 217  xml_char(void *arg, const XML_Char *p, int sz)
Line 290  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 225  pnode_trim(struct pnode *pn)
Line 299  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 247  static void
Line 321  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;
         enum attrkey      key;          enum attrkey      key;
         enum attrval      val;          enum attrval      val;
         struct pnode     *dat;          struct pnode     *dat;
         struct pattr     *pattr;          struct pattr     *pattr;
         const XML_Char  **att;          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 266  xml_elem_start(void *arg, const XML_Char *name, const 
Line 341  xml_elem_start(void *arg, const XML_Char *name, const 
                 ps->node = ps->cur->node;                  ps->node = ps->cur->node;
         }          }
   
         for (node = 0; node < NODE__MAX; node++)          for (node = nodes; NULL != node->name; node++)
                 if (NULL == nodes[node].name)                  if (0 == strcmp(node->name, name))
                         continue;  
                 else if (0 == strcmp(nodes[node].name, name))  
                         break;                          break;
   
         if (NODE__MAX == node && NODE_ROOT == ps->node) {          if (NULL == node->name) {
                 return;                  if (NODE_ROOT == ps->node)
         } else if (NODE__MAX == node) {                          return;
                 fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n",                  fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n",
                         ps->fname, XML_GetCurrentLineNumber(ps->xml),                          ps->fname, XML_GetCurrentLineNumber(ps->xml),
                         XML_GetCurrentColumnNumber(ps->xml), name);                          XML_GetCurrentColumnNumber(ps->xml), name);
                 ps->stop = 1;                  ps->stop = 1;
Line 286  xml_elem_start(void *arg, const XML_Char *name, const 
Line 359  xml_elem_start(void *arg, const XML_Char *name, const 
                         XML_GetCurrentColumnNumber(ps->xml));                          XML_GetCurrentColumnNumber(ps->xml));
                 ps->stop = 1;                  ps->stop = 1;
                 return;                  return;
         } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {  
                 return;  
         } else if ( ! isparent(node, ps->node)) {  
                 fprintf(stderr, "%s:%zu:%zu: bad parent \"%s\" "  
                         "of node \"%s\"\n",  
                         ps->fname, XML_GetCurrentLineNumber(ps->xml),  
                         XML_GetCurrentColumnNumber(ps->xml),  
                         NULL == nodes[ps->node].name ?  
                         "(none)" : nodes[ps->node].name,  
                         NULL == nodes[node].name ?  
                         "(none)" : nodes[node].name);  
                 ps->stop = 1;  
                 return;  
         }          }
   
           if (NODE_INLINEEQUATION == node->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);
         }          }
   
         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);          TAILQ_INIT(&dat->attrq);
Line 326  xml_elem_start(void *arg, const XML_Char *name, const 
Line 389  xml_elem_start(void *arg, const XML_Char *name, const 
                         if (0 == strcmp(*att, attrkeys[key]))                          if (0 == strcmp(*att, attrkeys[key]))
                                 break;                                  break;
                 if (ATTRKEY__MAX == key) {                  if (ATTRKEY__MAX == key) {
                         fprintf(stderr, "%s:%zu:%zu: unknown "                          if (warn)
                                 "attribute \"%s\"\n", ps->fname,                                  fprintf(stderr, "%s:%zu:%zu: warning: "
                                 XML_GetCurrentLineNumber(ps->xml),                                          "unknown attribute \"%s\"\n",
                                 XML_GetCurrentColumnNumber(ps->xml),                                          ps->fname,
                                 *att);                                          XML_GetCurrentLineNumber(ps->xml),
                                           XML_GetCurrentColumnNumber(ps->xml),
                                           *att);
                         continue;                          continue;
                 } else if ( ! isattrkey(node, key)) {  
                         fprintf(stderr, "%s:%zu:%zu: bad "  
                                 "attribute \"%s\"\n", ps->fname,  
                                 XML_GetCurrentLineNumber(ps->xml),  
                                 XML_GetCurrentColumnNumber(ps->xml),  
                                 *att);  
                         continue;  
                 }                  }
                 for (val = 0; val < ATTRVAL__MAX; val++)                  for (val = 0; val < ATTRVAL__MAX; val++)
                         if (0 == strcmp(*(att + 1), attrvals[val]))                          if (0 == strcmp(*(att + 1), attrvals[val]))
                                 break;                                  break;
                 if (ATTRVAL__MAX != val && ! isattrval(key, val)) {  
                         fprintf(stderr, "%s:%zu:%zu: bad "  
                                 "value \"%s\"\n", ps->fname,  
                                 XML_GetCurrentLineNumber(ps->xml),  
                                 XML_GetCurrentColumnNumber(ps->xml),  
                                 *(att + 1));  
                         continue;  
                 }  
                 pattr = calloc(1, sizeof(struct pattr));                  pattr = calloc(1, sizeof(struct pattr));
                 pattr->key = key;                  pattr->key = key;
                 pattr->val = val;                  pattr->val = val;
Line 371  xml_elem_end(void *arg, const XML_Char *name)
Line 421  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 412  pnode_free(struct pnode *pn)
Line 465  pnode_free(struct pnode *pn)
                 free(ap);                  free(ap);
         }          }
   
         free(pn->b);          free(pn->real);
         free(pn);          free(pn);
 }  }
   
Line 440  pnode_unlinksub(struct pnode *pn)
Line 493  pnode_unlinksub(struct pnode *pn)
 }  }
   
 /*  /*
    * 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;
   
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ap->key == key)
                           return ap->val;
           return(ATTRVAL__MAX);
   }
   
   /*
    * 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;
   
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ap->key == key)
                           return ATTRVAL__MAX == ap->val ? ap->rawval :
                               attrvals[ap->val];
           return(defval);
   }
   
   /*
  * Reset the lookaside buffer.   * Reset the lookaside buffer.
  */   */
 static void  static void
Line 452  bufclear(struct parse *p)
Line 536  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 488  bufappend_r(struct parse *p, struct pnode *pn)
Line 572  bufappend_r(struct parse *p, struct pnode *pn)
                 bufappend_r(p, pp);                  bufappend_r(p, pp);
 }  }
   
 /*  /*
  * Recursively search and return the first instance of "node".   * Recursively search and return the first instance of "node".
  */   */
 static struct pnode *  static struct pnode *
Line 509  pnode_findfirst(struct pnode *pn, enum nodeid node)
Line 593  pnode_findfirst(struct pnode *pn, enum nodeid node)
   
 #define MACROLINE_NORM  0  #define MACROLINE_NORM  0
 #define MACROLINE_UPPER 1  #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.
Line 518  pnode_printmacrolinetext(struct parse *p, struct pnode
Line 603  pnode_printmacrolinetext(struct parse *p, struct pnode
 {  {
         char            *cp;          char            *cp;
   
         if (0 == p->newln)          if (0 == p->newln && ! (MACROLINE_NOWS & fl))
                 putchar(' ');                  putchar(' ');
   
         bufclear(p);          bufclear(p);
Line 526  pnode_printmacrolinetext(struct parse *p, struct pnode
Line 611  pnode_printmacrolinetext(struct parse *p, struct pnode
   
         /* 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);
                 if (MACROLINE_UPPER & fl)                  if (MACROLINE_UPPER & fl)
                         putchar(toupper((int)*cp));                          putchar(toupper((unsigned char)*cp));
                 else                  else
                         putchar((int)*cp);                          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');
Line 595  pnode_printmclose(struct parse *p, int sv)
Line 680  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;
   }
   
   static void
   pnode_printpara(struct parse *p, struct pnode *pn)
   {
           struct pnode    *pp;
   
           assert(p->newln);
           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;
           }
           puts(".Pp");
   }
   
   /*
  * 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 602  pnode_printrefsynopsisdiv(struct parse *p, struct pnod
Line 747  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 616  static void
Line 761  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 (NULL == pn->parent)
                   return;
   
           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;
   
         if (NODE_REFSECT1 == pn->node)          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);                  fputs(".Sh", stdout);
         else                  break;
           case 2:
                 fputs(".Ss", stdout);                  fputs(".Ss", stdout);
                   break;
           default:
                   pnode_printpara(p, pn);
                   fputs(".Sy", stdout);
                   break;
           }
   
         p->newln = 0;  
   
         if (NULL != pp) {          if (NULL != pp) {
                 pnode_printmacrolinetext(p, pp,                  p->newln = 0;
                         NODE_REFSECT1 == pn->node ?                  pnode_printmacrolinetext(p, pp, flags);
                         MACROLINE_UPPER : 0);  
                 pnode_printmclose(p, 1);                  pnode_printmclose(p, 1);
                 pnode_unlink(pp);                  pnode_unlink(pp);
         } else {          } else
                 puts("UNKNOWN");                  printf(" %s\n", title);
                 p->newln = 1;  
         }  
 }  }
   
 /*  /*
Line 649  pnode_printciterefentry(struct parse *p, struct pnode 
Line 840  pnode_printciterefentry(struct parse *p, struct pnode 
         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;
   
         fputs(".Xr", stdout);  
         p->newln = 0;  
   
         if (NULL != title) {          if (NULL != title) {
                 pnode_printmacrolinepart(p, title);                  pnode_printmacrolinepart(p, title);
         } else          } else
Line 668  pnode_printciterefentry(struct parse *p, struct pnode 
Line 855  pnode_printciterefentry(struct parse *p, struct pnode 
                 puts(" 1");                  puts(" 1");
                 p->newln = 1;                  p->newln = 1;
         } else          } else
                 pnode_printmacroline(p, manvol);                  pnode_printmacrolinepart(p, manvol);
 }  }
   
 static void  static void
Line 684  pnode_printrefmeta(struct parse *p, struct pnode *pn)
Line 871  pnode_printrefmeta(struct parse *p, struct pnode *pn)
                 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;          p->newln = 0;
   
Line 698  pnode_printrefmeta(struct parse *p, struct pnode *pn)
Line 884  pnode_printrefmeta(struct parse *p, struct pnode *pn)
                 p->newln = 1;                  p->newln = 1;
         } else          } else
                 pnode_printmacroline(p, manvol);                  pnode_printmacroline(p, manvol);
   
         puts(".Os");  
 }  }
   
 static void  static void
Line 747  pnode_printparamdef(struct parse *p, struct pnode *pn)
Line 931  pnode_printparamdef(struct parse *p, struct pnode *pn)
         fputs(".Fa \"", stdout);          fputs(".Fa \"", stdout);
         p->newln = 0;          p->newln = 0;
         if (NULL != ptype) {          if (NULL != ptype) {
                 pnode_printmacrolinepart(p, ptype);                  pnode_printmacrolinetext(p, ptype, MACROLINE_NOWS);
                 putchar(' ');                  putchar(' ');
         }          }
   
Line 758  pnode_printparamdef(struct parse *p, struct pnode *pn)
Line 942  pnode_printparamdef(struct parse *p, struct pnode *pn)
         p->newln = 1;          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;
   
           printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "("));
   
           pp = TAILQ_FIRST(&pn->childq);
           pnode_print(p, pp);
   
           while (NULL != (pp = TAILQ_NEXT(pp, child))) {
                   putchar(',');
                   pnode_print(p, pp);
           }
   
           printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")"));
   }
   
   /*
    * 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);          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 773  pnode_printfuncprototype(struct parse *p, struct pnode
Line 1013  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);
   
Line 781  pnode_printfuncprototype(struct parse *p, struct pnode
Line 1021  pnode_printfuncprototype(struct parse *p, struct pnode
         p->newln = 1;          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").
Line 797  pnode_printarg(struct parse *p, struct pnode *pn)
Line 1037  pnode_printarg(struct parse *p, struct pnode *pn)
   
         isop = 1;          isop = 1;
         isrep = 0;          isrep = 0;
         TAILQ_FOREACH(ap, &pn->attrq, child)          TAILQ_FOREACH(ap, &pn->attrq, child)
                 if (ATTRKEY_CHOICE == ap->key &&                  if (ATTRKEY_CHOICE == ap->key &&
                         (ATTRVAL_PLAIN == ap->val ||                          (ATTRVAL_PLAIN == ap->val ||
                          ATTRVAL_REQ == ap->val))                           ATTRVAL_REQ == ap->val))
                         isop = 0;                          isop = 0;
                 else if (ATTRKEY_REP == ap->key &&                  else if (ATTRKEY_REP == ap->key &&
                         (ATTRVAL_REPEAT == ap->val))                          (ATTRVAL_REPEAT == ap->val))
Line 815  pnode_printarg(struct parse *p, struct pnode *pn)
Line 1055  pnode_printarg(struct parse *p, struct pnode *pn)
                 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)                  if (NODE_TEXT == pp->node && isrep)
                         fputs("...", stdout);                          fputs("...", stdout);
         }          }
 }  }
Line 830  pnode_printgroup(struct parse *p, struct pnode *pn)
Line 1070  pnode_printgroup(struct parse *p, struct pnode *pn)
         int              isop, sv;          int              isop, sv;
   
         isop = 1;          isop = 1;
         TAILQ_FOREACH(ap, &pn->attrq, child)          TAILQ_FOREACH(ap, &pn->attrq, child)
                 if (ATTRKEY_CHOICE == ap->key &&                  if (ATTRKEY_CHOICE == ap->key &&
                         (ATTRVAL_PLAIN == ap->val ||                          (ATTRVAL_PLAIN == ap->val ||
                          ATTRVAL_REQ == ap->val)) {                           ATTRVAL_REQ == ap->val)) {
Line 838  pnode_printgroup(struct parse *p, struct pnode *pn)
Line 1078  pnode_printgroup(struct parse *p, struct pnode *pn)
                         break;                          break;
                 }                  }
   
         /*          /*
          * Make sure we're on a macro line.           * Make sure we're on a macro line.
          * This will prevent pnode_print() for putting us on a           * This will prevent pnode_print() for putting us on a
          * subsequent line.           * subsequent line.
          */           */
         sv = p->newln;          sv = p->newln;
         pnode_printmopen(p);          pnode_printmopen(p);
         if (isop)          if (isop)
                 fputs("Op", stdout);                  fputs("Op", stdout);
         else if (sv)          else if (sv)
                 fputs("No", stdout);                  fputs("No", stdout);
Line 882  pnode_printprologue(struct parse *p, struct pnode *pn)
Line 1122  pnode_printprologue(struct parse *p, struct pnode *pn)
         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...");                  printf(".Dt %s 1\n",
                 puts(".Dd $Mdocdate" "$");                      pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN"));
                 puts(".Dt UNKNOWN 1");          puts(".Os");
                 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  static void
 pnode_printvarlistentry(struct parse *p, struct pnode *pn)  pnode_printvarlistentry(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           int              first = 1;
   
         assert(p->newln);          assert(p->newln);
           fputs(".It", stdout);
           p->newln = 0;
   
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TERM == pp->node) {                  if (NODE_TERM == pp->node) {
                         fputs(".It", stdout);                          if ( ! first)
                         p->newln = 0;                                  putchar(',');
                         pnode_print(p, pp);                          pnode_print(p, pp);
                         pnode_unlink(pp);                          pnode_unlink(pp);
                         pnode_printmclose(p, 1);                          first = 0;
                         return;                  } else
                 }                          break;
   
         puts(".It");          putchar('\n');
         p->newln = 1;          p->newln = 1;
 }  }
   
Line 938  pnode_printtable(struct parse *p, struct pnode *pn)
Line 1191  pnode_printtable(struct parse *p, struct pnode *pn)
         assert(p->newln);          assert(p->newln);
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TITLE == pp->node) {                  if (NODE_TITLE == pp->node) {
                         puts(".Pp");                          pnode_printpara(p, pp);
                         pnode_print(p, pp);                          pnode_print(p, pp);
                         pnode_unlink(pp);                          pnode_unlink(pp);
                 }                  }
Line 962  pnode_printlist(struct parse *p, struct pnode *pn)
Line 1215  pnode_printlist(struct parse *p, struct pnode *pn)
         assert(p->newln);          assert(p->newln);
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TITLE == pp->node) {                  if (NODE_TITLE == pp->node) {
                         puts(".Pp");                          pnode_printpara(p, pp);
                         pnode_print(p, pp);                          pnode_print(p, pp);
                         pnode_unlink(pp);                          pnode_unlink(pp);
                 }                  }
Line 971  pnode_printlist(struct parse *p, struct pnode *pn)
Line 1224  pnode_printlist(struct parse *p, struct pnode *pn)
         if (NODE_ORDEREDLIST == pn->node)          if (NODE_ORDEREDLIST == pn->node)
                 puts(".Bl -enum");                  puts(".Bl -enum");
         else          else
                 puts(".Bl -item");                  puts(".Bl -bullet");
   
         TAILQ_FOREACH(pp, &pn->childq, child) {          TAILQ_FOREACH(pp, &pn->childq, child) {
                 assert(p->newln);                  assert(p->newln);
Line 991  pnode_printvariablelist(struct parse *p, struct pnode 
Line 1244  pnode_printvariablelist(struct parse *p, struct pnode 
         assert(p->newln);          assert(p->newln);
         TAILQ_FOREACH(pp, &pn->childq, child)          TAILQ_FOREACH(pp, &pn->childq, child)
                 if (NODE_TITLE == pp->node) {                  if (NODE_TITLE == pp->node) {
                         puts(".Pp");                          pnode_printpara(p, pp);
                         pnode_print(p, pp);                          pnode_print(p, pp);
                         pnode_unlink(pp);                          pnode_unlink(pp);
                 }                  }
Line 1021  static void
Line 1274  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, sv;          int              last, sv;
   
Line 1030  pnode_print(struct parse *p, struct pnode *pn)
Line 1284  pnode_print(struct parse *p, struct pnode *pn)
         sv = p->newln;          sv = p->newln;
   
         switch (pn->node) {          switch (pn->node) {
         case (NODE_APPLICATION):          case NODE_APPLICATION:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Nm", stdout);                  fputs("Nm", stdout);
                 break;                  break;
         case (NODE_ARG):          case NODE_ANCHOR:
                   /* Don't print anything! */
                   return;
           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_EMPHASIS):          case NODE_CONSTANT:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                   fputs("Dv", stdout);
                   break;
           case NODE_EDITOR:
                   puts("editor: ");
                   pnode_printmopen(p);
                   fputs("An", stdout);
                   break;
           case NODE_EMPHASIS:
           case NODE_FIRSTTERM:
                   pnode_printmopen(p);
                 fputs("Em", stdout);                  fputs("Em", stdout);
                 break;                  break;
         case (NODE_ENVAR):          case NODE_ENVAR:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Ev", stdout);                  fputs("Ev", stdout);
                 break;                  break;
         case (NODE_FILENAME):          case NODE_FILENAME:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Pa", stdout);                  fputs("Pa", stdout);
                 break;                  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);
                 pnode_printfuncprototype(p, pn);                  pnode_printfuncprototype(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(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_ITEMIZEDLIST):          case NODE_INDEXTERM:
                   return;
           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);                  assert(p->newln);
                 pnode_printlist(p, pn);                  pnode_printlist(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_GROUP):          case NODE_GROUP:
                 pnode_printgroup(p, pn);                  pnode_printgroup(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_LITERAL):          case NODE_LEGALNOTICE:
                   assert(p->newln);
                   puts(".Sh LEGAL NOTICE");
                   break;
           case NODE_LINK:
                   ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL);
                   if (NULL == ccp)
                           break;
                 pnode_printmopen(p);                  pnode_printmopen(p);
                   printf("Sx %s\n", ccp);
                   p->newln = 1;
                   return;
           case NODE_LITERAL:
                   pnode_printmopen(p);
                 fputs("Li", stdout);                  fputs("Li", stdout);
                 break;                  break;
         case (NODE_OPTION):          case NODE_LITERALLAYOUT:
                   assert(p->newln);
                   puts(".Bd -literal");
                   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:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Fl", stdout);                  fputs("Fl", stdout);
                 break;                  break;
         case (NODE_ORDEREDLIST):          case NODE_ORDEREDLIST:
                 assert(p->newln);                  assert(p->newln);
                 pnode_printlist(p, pn);                  pnode_printlist(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_PARA):          case NODE_PARA:
                 assert(p->newln);                  pnode_printpara(p, pn);
                 if (NULL != pn->parent &&  
                         NODE_LISTITEM == pn->parent->node)  
                         break;  
                 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):          case NODE_QUOTE:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Qo", stdout);                  fputs("Qo", stdout);
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_PROGRAMLISTING:
                 /* FALLTHROUGH */          case NODE_SCREEN:
         case (NODE_SCREEN):  
                 assert(p->newln);                  assert(p->newln);
                 puts(".Bd -literal");                  printf(".Bd %s\n", ATTRVAL_MONOSPACED ==
                       pnode_getattr(pn, ATTRKEY_CLASS) ?
                       "-literal" : "-unfilled");
                 break;                  break;
         case (NODE_REFENTRYINFO):          case NODE_REFENTRYINFO:
                 /* Suppress. */                  /* Suppress. */
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  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);
Line 1138  pnode_print(struct parse *p, struct pnode *pn)
Line 1461  pnode_print(struct parse *p, struct pnode *pn)
                 pnode_printmacrolinepart(p, pn);                  pnode_printmacrolinepart(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_REFNAMEDIV):          case NODE_REFNAMEDIV:
                 assert(p->newln);                  assert(p->newln);
                 puts(".Sh NAME");                  puts(".Sh NAME");
                 break;                  break;
         case (NODE_REFPURPOSE):          case NODE_REFPURPOSE:
                 assert(p->newln);                  assert(p->newln);
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Nd", stdout);                  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_REFSECT1):          case NODE_PREFACE:
                 /* FALLTHROUGH */          case NODE_SECTION:
         case (NODE_REFSECT2):          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):          case NODE_REPLACEABLE:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Ar", stdout);                  fputs("Ar", stdout);
                 break;                  break;
         case (NODE_SBR):          case NODE_SBR:
                 assert(p->newln);                  assert(p->newln);
                 puts(".br");                  puts(".br");
                 break;                  break;
         case (NODE_STRUCTNAME):          case NODE_SGMLTAG:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                   fputs("Li", stdout);
                   break;
           case NODE_STRUCTNAME:
                   pnode_printmopen(p);
                 fputs("Vt", stdout);                  fputs("Vt", stdout);
                 break;                  break;
         case (NODE_TABLE):          case NODE_TABLE:
           case NODE_INFORMALTABLE:
                 assert(p->newln);                  assert(p->newln);
                 pnode_printtable(p, pn);                  pnode_printtable(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_TEXT):          case NODE_TEXT:
                 if (0 == p->newln)                  if (0 == p->newln)
                         putchar(' ');                          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);
                 cp = p->b;                  cp = p->b;
   
                 /*                  /*
                  * There's often a superfluous "-" in its <option> tags                   * There's often a superfluous "-" in its <option> tags
                  * before the actual flags themselves.                   * before the actual flags themselves.
Line 1200  pnode_print(struct parse *p, struct pnode *pn)
Line 1539  pnode_print(struct parse *p, struct pnode *pn)
                 for (last = '\n'; '\0' != *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 1214  pnode_print(struct parse *p, struct pnode *pn)
Line 1553  pnode_print(struct parse *p, struct pnode *pn)
                 }                  }
                 p->newln = 0;                  p->newln = 0;
                 break;                  break;
         case (NODE_USERINPUT):          case NODE_TITLE:
                   if (pn->parent->node == NODE_BOOKINFO) {
                           pnode_printmopen(p);
                           fputs("Nd", stdout);
                   }
                   break;
           case NODE_TYPE:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                   fputs("Vt", stdout);
                   break;
           case NODE_USERINPUT:
                   pnode_printmopen(p);
                 fputs("Li", stdout);                  fputs("Li", stdout);
                 break;                  break;
         case (NODE_VARIABLELIST):          case NODE_VARIABLELIST:
                 assert(p->newln);                  assert(p->newln);
                 pnode_printvariablelist(p, pn);                  pnode_printvariablelist(p, pn);
                 pnode_unlinksub(pn);                  pnode_unlinksub(pn);
                 break;                  break;
         case (NODE_VARLISTENTRY):          case NODE_VARLISTENTRY:
                 assert(p->newln);                  assert(p->newln);
                 pnode_printvarlistentry(p, pn);                  pnode_printvarlistentry(p, pn);
                 break;                  break;
         case (NODE_VARNAME):          case NODE_VARNAME:
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Va", stdout);                  fputs("Va", stdout);
                 break;                  break;
Line 1239  pnode_print(struct parse *p, struct pnode *pn)
Line 1588  pnode_print(struct parse *p, struct pnode *pn)
                 pnode_print(p, pp);                  pnode_print(p, pp);
   
         switch (pn->node) {          switch (pn->node) {
         case (NODE_APPLICATION):          case NODE_INFORMALEQUATION:
         case (NODE_ARG):                  if ( ! p->newln)
         case (NODE_CODE):                          putchar('\n');
         case (NODE_COMMAND):                  puts(".EN");
         case (NODE_EMPHASIS):                  p->newln = 1;
         case (NODE_ENVAR):  
         case (NODE_FILENAME):  
         case (NODE_FUNCTION):  
         case (NODE_FUNCSYNOPSISINFO):  
         case (NODE_LITERAL):  
         case (NODE_OPTION):  
         case (NODE_PARAMETER):  
         case (NODE_REPLACEABLE):  
         case (NODE_REFPURPOSE):  
         case (NODE_STRUCTNAME):  
         case (NODE_TEXT):  
         case (NODE_USERINPUT):  
         case (NODE_VARNAME):  
                 pnode_printmclose(p, sv);  
                 break;                  break;
         case (NODE_QUOTE):          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_AUTHOR:
           case NODE_CITEREFENTRY:
           case NODE_CODE:
           case NODE_COMMAND:
           case NODE_CONSTANT:
           case NODE_EDITOR:
           case NODE_EMPHASIS:
           case NODE_ENVAR:
           case NODE_FILENAME:
           case NODE_FIRSTTERM:
           case NODE_FUNCTION:
           case NODE_FUNCSYNOPSISINFO:
           case NODE_LITERAL:
           case NODE_OPTION:
           case NODE_PARAMETER:
           case NODE_REPLACEABLE:
           case NODE_REFPURPOSE:
           case NODE_SGMLTAG:
           case NODE_STRUCTNAME:
           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;                  sv = p->newln;
                 pnode_printmopen(p);                  pnode_printmopen(p);
                 fputs("Qc", stdout);                  fputs("Qc", stdout);
                 pnode_printmclose(p, sv);                  pnode_printmclose(p, sv);
                 break;                  break;
         case (NODE_REFNAME):          case NODE_REFNAME:
                 /*                  /*
                  * If we're in the NAME macro and we have multiple                   * If we're in the NAME macro and we have multiple
                  * <refname> macros in sequence, then print out a                   * <refname> macros in sequence, then print out a
                  * trailing comma before the newline.                   * trailing comma before the newline.
                  */                   */
                 if (NULL != pn->parent &&                  if (NULL != pn->parent &&
                         NODE_REFNAMEDIV == pn->parent->node &&                          NODE_REFNAMEDIV == pn->parent->node &&
                         NULL != TAILQ_NEXT(pn, child) &&                          NULL != TAILQ_NEXT(pn, child) &&
                         NODE_REFNAME == TAILQ_NEXT(pn, child)->node)                          NODE_REFNAME == TAILQ_NEXT(pn, child)->node)
                         fputs(" ,", stdout);                          fputs(" ,", stdout);
                 pnode_printmclose(p, sv);                  pnode_printmclose(p, sv);
                 break;                  break;
         case (NODE_PROGRAMLISTING):          case NODE_PREFACE:
                 /* FALLTHROUGH */          case NODE_SECTION:
         case (NODE_SCREEN):          case NODE_NOTE:
           case NODE_TIP:
           case NODE_CAUTION:
           case NODE_WARNING:
                   p->level--;
                   break;
           case NODE_LITERALLAYOUT:
           case NODE_PROGRAMLISTING:
           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 1296  pnode_print(struct parse *p, struct pnode *pn)
Line 1684  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 1315  readfile(XML_Parser xp, int fd, 
Line 1703  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 1345  main(int argc, char *argv[])
Line 1735  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 1387  main(int argc, char *argv[])
Line 1791  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.28  
changed lines
  Added in v.1.62

CVSweb