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

Diff for /docbook2mdoc/parse.c between version 1.18 and 1.50

version 1.18, 2019/04/07 17:00:56 version 1.50, 2019/04/23 15:47:23
Line 17 
Line 17 
  */   */
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <libgen.h>
 #include <stdarg.h>  #include <stdarg.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
Line 45  enum pstate {
Line 48  enum pstate {
 struct  parse {  struct  parse {
         const char      *fname;  /* Name of the input file. */          const char      *fname;  /* Name of the input file. */
         struct ptree    *tree;   /* Complete parse result. */          struct ptree    *tree;   /* Complete parse result. */
           struct pnode    *doctype;
         struct pnode    *cur;    /* Current node in the tree. */          struct pnode    *cur;    /* Current node in the tree. */
         enum nodeid      ncur;   /* Type of the current node. */          enum nodeid      ncur;   /* Type of the current node. */
         int              line;   /* Line number in the input file. */          int              line;   /* Line number in the input file. */
Line 52  struct parse {
Line 56  struct parse {
         int              nline;  /* Line number of next token. */          int              nline;  /* Line number of next token. */
         int              ncol;   /* Column number of next token. */          int              ncol;   /* Column number of next token. */
         int              del;    /* Levels of nested nodes being deleted. */          int              del;    /* Levels of nested nodes being deleted. */
         int              spc;    /* Whitespace before the next element. */          int              nofill; /* Levels of open no-fill displays. */
         int              attr;   /* The most recent attribute is valid. */          int              flags;
         int              warn;  #define PFLAG_WARN       (1 << 0)  /* Print warning messages. */
   #define PFLAG_SPC        (1 << 1)  /* Whitespace before the next element. */
   #define PFLAG_ATTR       (1 << 2)  /* The most recent attribute is valid. */
   #define PFLAG_EEND       (1 << 3)  /* This element is self-closing. */
 };  };
   
 struct  element {  struct  alias {
         const char      *name;   /* DocBook element name. */          const char      *name;   /* DocBook element name. */
         enum nodeid      node;   /* Node type to generate. */          enum nodeid      node;   /* Node type to generate. */
 };  };
   
 static  const struct element elements[] = {  static  const struct alias aliases[] = {
         { "acronym",            NODE_IGNORE },          { "acronym",            NODE_IGNORE },
         { "affiliation",        NODE_AFFILIATION },          { "affiliation",        NODE_IGNORE },
         { "anchor",             NODE_DELETE },          { "anchor",             NODE_DELETE },
         { "application",        NODE_APPLICATION },          { "application",        NODE_COMMAND },
         { "arg",                NODE_ARG },          { "article",            NODE_SECTION },
         { "author",             NODE_AUTHOR },          { "articleinfo",        NODE_BOOKINFO },
         { "authorgroup",        NODE_AUTHORGROUP },          { "book",               NODE_SECTION },
         { "blockquote",         NODE_BLOCKQUOTE },  
         { "book",               NODE_BOOK },  
         { "bookinfo",           NODE_BOOKINFO },  
         { "caution",            NODE_CAUTION },  
         { "chapter",            NODE_SECTION },          { "chapter",            NODE_SECTION },
         { "citerefentry",       NODE_CITEREFENTRY },          { "caption",            NODE_IGNORE },
         { "citetitle",          NODE_CITETITLE },  
         { "cmdsynopsis",        NODE_CMDSYNOPSIS },  
         { "code",               NODE_LITERAL },          { "code",               NODE_LITERAL },
         { "colspec",            NODE_COLSPEC },          { "computeroutput",     NODE_LITERAL },
         { "command",            NODE_COMMAND },          { "!doctype",           NODE_DOCTYPE },
         { "constant",           NODE_CONSTANT },          { "figure",             NODE_IGNORE },
         { "contrib",            NODE_CONTRIB },  
         { "copyright",          NODE_COPYRIGHT },  
         { "date",               NODE_DATE },  
         { "editor",             NODE_EDITOR },  
         { "email",              NODE_EMAIL },  
         { "emphasis",           NODE_EMPHASIS },  
         { "entry",              NODE_ENTRY },  
         { "envar",              NODE_ENVAR },  
         { "errorname",          NODE_ERRORNAME },  
         { "fieldsynopsis",      NODE_FIELDSYNOPSIS },  
         { "filename",           NODE_FILENAME },  
         { "firstname",          NODE_PERSONNAME },          { "firstname",          NODE_PERSONNAME },
         { "firstterm",          NODE_FIRSTTERM },          { "glossary",           NODE_VARIABLELIST },
         { "footnote",           NODE_FOOTNOTE },          { "glossdef",           NODE_IGNORE },
         { "funcdef",            NODE_FUNCDEF },          { "glossdiv",           NODE_IGNORE },
         { "funcprototype",      NODE_FUNCPROTOTYPE },          { "glossentry",         NODE_VARLISTENTRY },
         { "funcsynopsis",       NODE_FUNCSYNOPSIS },          { "glosslist",          NODE_VARIABLELIST },
         { "funcsynopsisinfo",   NODE_FUNCSYNOPSISINFO },          { "holder",             NODE_IGNORE },
         { "function",           NODE_FUNCTION },          { "imageobject",        NODE_IGNORE },
         { "glossterm",          NODE_GLOSSTERM },  
         { "group",              NODE_GROUP },  
         { "holder",             NODE_HOLDER },  
         { "index",              NODE_INDEX },  
         { "indexterm",          NODE_DELETE },          { "indexterm",          NODE_DELETE },
         { "info",               NODE_INFO },  
         { "informalequation",   NODE_INFORMALEQUATION },  
         { "informaltable",      NODE_TABLE },          { "informaltable",      NODE_TABLE },
         { "inlineequation",     NODE_INLINEEQUATION },          { "keycap",             NODE_KEYSYM },
         { "itemizedlist",       NODE_ITEMIZEDLIST },          { "keycode",            NODE_IGNORE },
         { "keysym",             NODE_KEYSYM },          { "mediaobject",        NODE_BLOCKQUOTE },
         { "legalnotice",        NODE_LEGALNOTICE },          { "orgname",            NODE_IGNORE },
         { "link",               NODE_LINK },          { "othercredit",        NODE_AUTHOR },
         { "listitem",           NODE_LISTITEM },  
         { "literal",            NODE_LITERAL },  
         { "literallayout",      NODE_LITERALLAYOUT },  
         { "manvolnum",          NODE_MANVOLNUM },  
         { "member",             NODE_MEMBER },  
         { "mml:math",           NODE_MML_MATH },  
         { "mml:mfenced",        NODE_MML_MFENCED },  
         { "mml:mfrac",          NODE_MML_MFRAC },  
         { "mml:mi",             NODE_MML_MI },  
         { "mml:mn",             NODE_MML_MN },  
         { "mml:mo",             NODE_MML_MO },  
         { "mml:mrow",           NODE_MML_MROW },  
         { "mml:msub",           NODE_MML_MSUB },  
         { "mml:msup",           NODE_MML_MSUP },  
         { "modifier",           NODE_MODIFIER },  
         { "note",               NODE_NOTE },  
         { "option",             NODE_OPTION },  
         { "orderedlist",        NODE_ORDEREDLIST },  
         { "orgname",            NODE_ORGNAME },  
         { "othername",          NODE_PERSONNAME },          { "othername",          NODE_PERSONNAME },
         { "para",               NODE_PARA },  
         { "paramdef",           NODE_PARAMDEF },  
         { "parameter",          NODE_PARAMETER },  
         { "part",               NODE_SECTION },          { "part",               NODE_SECTION },
         { "personname",         NODE_PERSONNAME },  
         { "phrase",             NODE_IGNORE },          { "phrase",             NODE_IGNORE },
         { "preface",            NODE_PREFACE },  
         { "primary",            NODE_DELETE },          { "primary",            NODE_DELETE },
         { "programlisting",     NODE_PROGRAMLISTING },          { "property",           NODE_PARAMETER },
         { "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 },          { "refsect1",           NODE_SECTION },
         { "refsect2",           NODE_SECTION },          { "refsect2",           NODE_SECTION },
         { "refsect3",           NODE_SECTION },          { "refsect3",           NODE_SECTION },
         { "refsection",         NODE_SECTION },          { "refsection",         NODE_SECTION },
         { "refsynopsisdiv",     NODE_REFSYNOPSISDIV },          { "releaseinfo",        NODE_IGNORE },
         { "releaseinfo",        NODE_RELEASEINFO },          { "returnvalue",        NODE_IGNORE },
         { "replaceable",        NODE_REPLACEABLE },  
         { "row",                NODE_ROW },  
         { "sbr",                NODE_SBR },  
         { "screen",             NODE_SCREEN },  
         { "secondary",          NODE_DELETE },          { "secondary",          NODE_DELETE },
         { "sect1",              NODE_SECTION },          { "sect1",              NODE_SECTION },
         { "sect2",              NODE_SECTION },          { "sect2",              NODE_SECTION },
         { "section",            NODE_SECTION },          { "sect3",              NODE_SECTION },
         { "sgmltag",            NODE_SGMLTAG },          { "sect4",              NODE_SECTION },
           { "sgmltag",            NODE_MARKUP },
         { "simpara",            NODE_PARA },          { "simpara",            NODE_PARA },
         { "simplelist",         NODE_SIMPLELIST },  
         { "spanspec",           NODE_SPANSPEC },  
         { "structfield",        NODE_PARAMETER },          { "structfield",        NODE_PARAMETER },
         { "structname",         NODE_TYPE },          { "structname",         NODE_TYPE },
         { "subtitle",           NODE_SUBTITLE },  
         { "surname",            NODE_PERSONNAME },          { "surname",            NODE_PERSONNAME },
         { "symbol",             NODE_CONSTANT },          { "symbol",             NODE_CONSTANT },
         { "synopsis",           NODE_SYNOPSIS },          { "tag",                NODE_MARKUP },
         { "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_IGNORE },          { "trademark",          NODE_IGNORE },
         { "type",               NODE_TYPE },  
         { "ulink",              NODE_LINK },          { "ulink",              NODE_LINK },
         { "userinput",          NODE_LITERAL },          { "userinput",          NODE_LITERAL },
         { "variablelist",       NODE_VARIABLELIST },          { "year",               NODE_IGNORE },
         { "varlistentry",       NODE_VARLISTENTRY },  
         { "varname",            NODE_VARNAME },  
         { "warning",            NODE_WARNING },  
         { "wordasword",         NODE_WORDASWORD },  
         { "xi:include",         NODE_DELETE_WARN },  
         { "year",               NODE_YEAR },  
         { NULL,                 NODE_IGNORE }          { NULL,                 NODE_IGNORE }
 };  };
   
Line 258  static const struct entity entities[] = {
Line 184  static const struct entity entities[] = {
         { NULL,         NULL }          { NULL,         NULL }
 };  };
   
   static size_t    parse_string(struct parse *, char *, size_t,
                            enum pstate *, int);
   static void      parse_fd(struct parse *, int);
   
   
 static void  static void
   fatal(struct parse *p)
   {
           fprintf(stderr, "%s:%d:%d: FATAL: ", p->fname, p->line, p->col);
           perror(NULL);
           exit(6);
   }
   
   static void
 error_msg(struct parse *p, const char *fmt, ...)  error_msg(struct parse *p, const char *fmt, ...)
 {  {
         va_list          ap;          va_list          ap;
   
         fprintf(stderr, "%s:%d:%d: ", p->fname, p->line, p->col);          fprintf(stderr, "%s:%d:%d: ERROR: ", p->fname, p->line, p->col);
         va_start(ap, fmt);          va_start(ap, fmt);
         vfprintf(stderr, fmt, ap);          vfprintf(stderr, fmt, ap);
         va_end(ap);          va_end(ap);
         fputc('\n', stderr);          fputc('\n', stderr);
         p->tree->flags |= TREE_FAIL;          p->tree->flags |= TREE_ERROR;
 }  }
   
 static void  static void
Line 276  warn_msg(struct parse *p, const char *fmt, ...)
Line 215  warn_msg(struct parse *p, const char *fmt, ...)
 {  {
         va_list          ap;          va_list          ap;
   
         if (p->warn == 0)          if ((p->flags & PFLAG_WARN) == 0)
                 return;                  return;
   
         fprintf(stderr, "%s:%d:%d: warning: ", p->fname, p->line, p->col);          fprintf(stderr, "%s:%d:%d: WARNING: ", p->fname, p->line, p->col);
         va_start(ap, fmt);          va_start(ap, fmt);
         vfprintf(stderr, fmt, ap);          vfprintf(stderr, fmt, ap);
         va_end(ap);          va_end(ap);
         fputc('\n', stderr);          fputc('\n', stderr);
           p->tree->flags |= TREE_WARN;
 }  }
   
 /*  /*
Line 292  warn_msg(struct parse *p, const char *fmt, ...)
Line 232  warn_msg(struct parse *p, const char *fmt, ...)
  * Otherwise, create a new one as a child of the current node.   * Otherwise, create a new one as a child of the current node.
  */   */
 static void  static void
 xml_char(struct parse *ps, const char *p, int sz)  xml_text(struct parse *p, const char *word, int sz)
 {  {
         struct pnode    *dat;          struct pnode    *n, *np;
         size_t           newsz;          size_t           oldsz, newsz;
           int              i;
   
         if (ps->del > 0)          assert(sz > 0);
           if (p->del > 0)
                 return;                  return;
   
         if (ps->cur == NULL) {          if ((n = p->cur) == NULL) {
                 error_msg(ps, "discarding text before document: %.*s", sz, p);                  error_msg(p, "discarding text before document: %.*s",
                       sz, word);
                 return;                  return;
         }          }
   
         if (ps->cur->node != NODE_TEXT) {          /* Append to the current text node, if one is open. */
                 if ((dat = calloc(1, sizeof(*dat))) == NULL) {  
                         perror(NULL);          if (n->node == NODE_TEXT) {
                         exit(1);                  oldsz = strlen(n->b);
                 }                  newsz = oldsz + sz;
                 dat->node = NODE_TEXT;                  if (oldsz && (p->flags & PFLAG_SPC))
                 dat->spc = ps->spc;                          newsz++;
                 dat->parent = ps->cur;                  if ((n->b = realloc(n->b, newsz + 1)) == NULL)
                 TAILQ_INIT(&dat->childq);                          fatal(p);
                 TAILQ_INIT(&dat->attrq);                  if (oldsz && (p->flags & PFLAG_SPC))
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);                          n->b[oldsz++] = ' ';
                 ps->cur = dat;                  memcpy(n->b + oldsz, word, sz);
                   n->b[newsz] = '\0';
                   p->flags &= ~PFLAG_SPC;
                   return;
         }          }
   
         if (ps->tree->flags & TREE_CLOSED &&          if (p->tree->flags & TREE_CLOSED && n == p->tree->root)
             ps->cur->parent == ps->tree->root)                  warn_msg(p, "text after end of document: %.*s", sz, word);
                 warn_msg(ps, "text after end of document: %.*s", sz, p);  
   
         /* Append to the current text node. */          /* Create a new text node. */
   
         assert(sz >= 0);          if ((n = pnode_alloc(p->cur)) == NULL)
         newsz = ps->cur->bsz + (ps->cur->bsz && ps->spc) + sz;                  fatal(p);
         ps->cur->b = realloc(ps->cur->b, newsz + 1);          n->node = NODE_TEXT;
         if (ps->cur->b == NULL) {          n->spc = (p->flags & PFLAG_SPC) != 0;
                 perror(NULL);          p->flags &= ~PFLAG_SPC;
                 exit(1);  
           /*
            * If this node follows an in-line macro without intervening
            * whitespace, keep the text in it as short as possible,
            * and do not keep it open.
            */
   
           np = n->spc ? NULL : TAILQ_PREV(n, pnodeq, child);
           while (np != NULL) {
                   switch (pnode_class(np->node)) {
                   case CLASS_VOID:
                   case CLASS_TEXT:
                   case CLASS_BLOCK:
                   case CLASS_NOFILL:
                           np = NULL;
                           break;
                   case CLASS_TRANS:
                           np = TAILQ_LAST(&np->childq, pnodeq);
                           continue;
                   case CLASS_LINE:
                   case CLASS_ENCL:
                           break;
                   }
                   break;
         }          }
         if (ps->cur->bsz && ps->spc)          if (np != NULL) {
                 ps->cur->b[ps->cur->bsz++] = ' ';                  i = 0;
         memcpy(ps->cur->b + ps->cur->bsz, p, sz);                  while (i < sz && !isspace((unsigned char)word[i]))
         ps->cur->b[ps->cur->bsz = newsz] = '\0';                          i++;
         ps->cur->real = ps->cur->b;                  if ((n->b = strndup(word, i)) == NULL)
         ps->spc = 0;                          fatal(p);
                   if (i == sz)
                           return;
                   while (i < sz && isspace((unsigned char)word[i]))
                           i++;
                   if (i == sz) {
                           p->flags |= PFLAG_SPC;
                           return;
                   }
   
                   /* Put any remaining text into a second node. */
   
                   if ((n = pnode_alloc(p->cur)) == NULL)
                           fatal(p);
                   n->node = NODE_TEXT;
                   n->spc = 1;
                   word += i;
                   sz -= i;
           }
           if ((n->b = strndup(word, sz)) == NULL)
                   fatal(p);
   
           /* The new node remains open for later pnode_closetext(). */
   
           p->cur = n;
 }  }
   
 /*  /*
  * Close out the text node and strip trailing whitespace, if one is open.   * Close out the text node and strip trailing whitespace, if one is open.
  */   */
 static void  static void
 pnode_closetext(struct parse *p)  pnode_closetext(struct parse *p, int check_last_word)
 {  {
         struct pnode    *n;          struct pnode    *n;
           char            *cp, *last_word;
   
         if ((n = p->cur) == NULL || n->node != NODE_TEXT)          if ((n = p->cur) == NULL || n->node != NODE_TEXT)
                 return;                  return;
         p->cur = n->parent;          p->cur = n->parent;
         while (n->bsz > 0 && isspace((unsigned char)n->b[n->bsz - 1])) {          for (cp = strchr(n->b, '\0');
                 n->b[--n->bsz] = '\0';              cp > n->b && isspace((unsigned char)cp[-1]);
                 p->spc = 1;              *--cp = '\0')
         }                  p->flags |= PFLAG_SPC;
   
           if (p->flags & PFLAG_SPC || !check_last_word)
                   return;
   
           /*
            * Find the beginning of the last word
            * and delete whitespace before it.
            */
   
           while (cp > n->b && !isspace((unsigned char)cp[-1]))
                   cp--;
           if (cp == n->b)
                   return;
   
           last_word = cp;
           while (cp > n->b && isspace((unsigned char)cp[-1]))
               *--cp = '\0';
   
           /* Move the last word into its own node, for use with .Pf. */
   
           if ((n = pnode_alloc(p->cur)) == NULL)
                   fatal(p);
           n->node = NODE_TEXT;
           n->spc = 1;
           if ((n->b = strdup(last_word)) == NULL)
                   fatal(p);
 }  }
   
 static void  static void
 xml_entity(struct parse *p, const char *name)  xml_entity(struct parse *p, const char *name)
 {  {
         const struct entity     *entity;          const struct entity     *entity;
         struct pnode            *dat;          struct pnode            *n;
           const char              *ccp;
           char                    *cp;
           unsigned int             codepoint;
           enum pstate              pstate;
   
         if (p->del > 0)          if (p->del > 0)
                 return;                  return;
Line 371  xml_entity(struct parse *p, const char *name)
Line 394  xml_entity(struct parse *p, const char *name)
                 return;                  return;
         }          }
   
         pnode_closetext(p);          pnode_closetext(p, 0);
   
         if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root)          if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root)
                 warn_msg(p, "entity after end of document: &%s;", name);                  warn_msg(p, "entity after end of document: &%s;", name);
Line 381  xml_entity(struct parse *p, const char *name)
Line 404  xml_entity(struct parse *p, const char *name)
                         break;                          break;
   
         if (entity->roff == NULL) {          if (entity->roff == NULL) {
                   if (p->doctype != NULL) {
                           TAILQ_FOREACH(n, &p->doctype->childq, child) {
                                   if ((ccp = pnode_getattr_raw(n,
                                        ATTRKEY_NAME, NULL)) == NULL ||
                                       strcmp(ccp, name) != 0)
                                           continue;
                                   if ((ccp = pnode_getattr_raw(n,
                                       ATTRKEY_SYSTEM, NULL)) != NULL) {
                                           parse_file(p, -1, ccp);
                                           p->flags &= ~PFLAG_SPC;
                                           return;
                                   }
                                   if ((ccp = pnode_getattr_raw(n,
                                        ATTRKEY_DEFINITION, NULL)) == NULL)
                                           continue;
                                   if ((cp = strdup(ccp)) == NULL)
                                           fatal(p);
                                   pstate = PARSE_ELEM;
                                   parse_string(p, cp, strlen(cp), &pstate, 0);
                                   p->flags &= ~PFLAG_SPC;
                                   free(cp);
                                   return;
                           }
                   }
                   if (*name == '#') {
                           codepoint = strtonum(name + 1, 0, 0x10ffff, &ccp);
                           if (ccp == NULL) {
                                   if ((n = pnode_alloc(p->cur)) == NULL ||
                                       asprintf(&n->b, "\\[u%4.4X]",
                                       codepoint) < 0)
                                           fatal(p);
                                   goto done;
                           }
                   }
                 error_msg(p, "unknown entity &%s;", name);                  error_msg(p, "unknown entity &%s;", name);
                 return;                  return;
         }          }
   
         /* Create, append, and close out an entity node. */          /* Create, append, and close out an entity node. */
         if ((dat = calloc(1, sizeof(*dat))) == NULL ||          if ((n = pnode_alloc(p->cur)) == NULL ||
             (dat->b = dat->real = strdup(entity->roff)) == NULL) {              (n->b = strdup(entity->roff)) == NULL)
                 perror(NULL);                  fatal(p);
                 exit(1);  done:
         }          n->node = NODE_ESCAPE;
         dat->node = NODE_ESCAPE;          n->spc = (p->flags & PFLAG_SPC) != 0;
         dat->bsz = strlen(dat->b);          p->flags &= ~PFLAG_SPC;
         dat->spc = p->spc;  
         dat->parent = p->cur;  
         TAILQ_INIT(&dat->childq);  
         TAILQ_INIT(&dat->attrq);  
         TAILQ_INSERT_TAIL(&p->cur->childq, dat, child);  
         p->spc = 0;  
 }  }
   
 /*  /*
    * Parse an element name.
    */
   static enum nodeid
   xml_name2node(struct parse *p, const char *name)
   {
           const struct alias      *alias;
           enum nodeid              node;
   
           if ((node = pnode_parse(name)) < NODE_UNKNOWN)
                   return node;
   
           for (alias = aliases; alias->name != NULL; alias++)
                   if (strcmp(alias->name, name) == 0)
                           return alias->node;
   
           return NODE_UNKNOWN;
   }
   
   /*
  * Begin an element.   * Begin an element.
  */   */
 static void  static void
 xml_elem_start(struct parse *ps, const char *name)  xml_elem_start(struct parse *p, const char *name)
 {  {
         const struct element    *elem;          struct pnode            *n;
         struct pnode            *dat;  
   
         if (*name == '!' || *name == '?')  
                 return;  
   
         /*          /*
          * An ancestor is excluded from the tree;           * An ancestor is excluded from the tree;
          * keep track of the number of levels excluded.           * keep track of the number of levels excluded.
          */           */
         if (ps->del > 0) {          if (p->del > 0) {
                 ps->del++;                  if (*name != '!' && *name != '?')
                           p->del++;
                 return;                  return;
         }          }
   
         pnode_closetext(ps);          switch (p->ncur = xml_name2node(p, name)) {
   
         for (elem = elements; elem->name != NULL; elem++)  
                 if (strcmp(elem->name, name) == 0)  
                         break;  
   
         if (elem->name == NULL)  
                 error_msg(ps, "unknown element <%s>", name);  
   
         ps->ncur = elem->node;  
   
         switch (ps->ncur) {  
         case NODE_DELETE_WARN:          case NODE_DELETE_WARN:
                 warn_msg(ps, "skipping element <%s>", name);                  warn_msg(p, "skipping element <%s>", name);
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case NODE_DELETE:          case NODE_DELETE:
                 ps->del = 1;                  p->del = 1;
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case NODE_IGNORE:          case NODE_IGNORE:
                 return;                  return;
         case NODE_INLINEEQUATION:          case NODE_UNKNOWN:
                 ps->tree->flags |= TREE_EQN;                  if (*name != '!' && *name != '?')
                 break;                          error_msg(p, "unknown element <%s>", name);
                   return;
         default:          default:
                 break;                  break;
         }          }
   
         if (ps->tree->flags & TREE_CLOSED && ps->cur->parent == NULL)          if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL)
                 warn_msg(ps, "element after end of document: <%s>", name);                  warn_msg(p, "element after end of document: <%s>", name);
   
         if ((dat = calloc(1, sizeof(*dat))) == NULL) {          switch (pnode_class(p->ncur)) {
                 perror(NULL);          case CLASS_LINE:
                 exit(1);          case CLASS_ENCL:
                   pnode_closetext(p, 1);
                   break;
           default:
                   pnode_closetext(p, 0);
                   break;
         }          }
   
           if ((n = pnode_alloc(p->cur)) == NULL)
                   fatal(p);
   
         /*          /*
            * Some elements are self-closing.
          * Nodes that begin a new macro or request line or start by           * Nodes that begin a new macro or request line or start by
          * printing text always want whitespace before themselves.           * printing text always want whitespace before themselves.
          */           */
   
         switch (dat->node = elem->node) {          switch (n->node = p->ncur) {
         case NODE_AUTHORGROUP:          case NODE_DOCTYPE:
         case NODE_BOOKINFO:          case NODE_ENTITY:
         case NODE_CAUTION:  
         case NODE_EDITOR:  
         case NODE_ENTRY:  
         case NODE_FUNCDEF:  
         case NODE_FUNCPROTOTYPE:  
         case NODE_INFORMALEQUATION:  
         case NODE_INLINEEQUATION:  
         case NODE_ITEMIZEDLIST:  
         case NODE_LEGALNOTICE:  
         case NODE_LITERALLAYOUT:  
         case NODE_NOTE:  
         case NODE_ORDEREDLIST:  
         case NODE_PARA:  
         case NODE_PREFACE:  
         case NODE_PROGRAMLISTING:  
         case NODE_REFMETA:  
         case NODE_REFNAMEDIV:  
         case NODE_REFSYNOPSISDIV:  
         case NODE_ROW:  
         case NODE_SBR:          case NODE_SBR:
         case NODE_SCREEN:          case NODE_VOID:
         case NODE_SECTION:                  p->flags |= PFLAG_EEND;
         case NODE_SYNOPSIS:  
         case NODE_TGROUP:  
         case NODE_TIP:  
         case NODE_TITLE:  
         case NODE_VARIABLELIST:  
         case NODE_VARLISTENTRY:  
         case NODE_WARNING:  
                 dat->spc = 1;  
                 break;                  break;
         default:          default:
                 dat->spc = ps->spc;  
                 break;                  break;
         }          }
         dat->parent = ps->cur;          switch (pnode_class(p->ncur)) {
         TAILQ_INIT(&dat->childq);          case CLASS_LINE:
         TAILQ_INIT(&dat->attrq);          case CLASS_ENCL:
                   n->spc = (p->flags & PFLAG_SPC) != 0;
         if (ps->cur != NULL)                  break;
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);          case CLASS_NOFILL:
                   p->nofill++;
         ps->cur = dat;                  /* FALLTHROUGH */
         if (ps->tree->root == NULL)          default:
                 ps->tree->root = dat;                  n->spc = 1;
                   break;
           }
           p->cur = n;
           if (n->node == NODE_DOCTYPE) {
                   if (p->doctype == NULL)
                           p->doctype = n;
                   else
                           error_msg(p, "duplicate doctype");
           } else if (n->parent == NULL && p->tree->root == NULL)
                   p->tree->root = n;
 }  }
   
 static void  static void
 xml_attrkey(struct parse *ps, const char *name)  xml_attrkey(struct parse *p, const char *name)
 {  {
         struct pattr    *attr;          struct pattr    *a;
           const char      *value;
         enum attrkey     key;          enum attrkey     key;
   
         if (ps->del > 0 || *name == '\0')          if (p->del > 0 || p->ncur >= NODE_UNKNOWN || *name == '\0')
                 return;                  return;
   
           if ((p->ncur == NODE_DOCTYPE || p->ncur == NODE_ENTITY) &&
               TAILQ_FIRST(&p->cur->attrq) == NULL) {
                   value = name;
                   name = "NAME";
           } else
                   value = NULL;
   
         if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {          if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
                 ps->attr = 0;                  p->flags &= ~PFLAG_ATTR;
                 return;                  return;
         }          }
         if ((attr = calloc(1, sizeof(*attr))) == NULL) {          if ((a = calloc(1, sizeof(*a))) == NULL)
                 perror(NULL);                  fatal(p);
                 exit(1);  
           a->key = key;
           a->val = ATTRVAL__MAX;
           if (value == NULL) {
                   a->rawval = NULL;
                   p->flags |= PFLAG_ATTR;
           } else {
                   if ((a->rawval = strdup(value)) == NULL)
                           fatal(p);
                   p->flags &= ~PFLAG_ATTR;
         }          }
         attr->key = key;          TAILQ_INSERT_TAIL(&p->cur->attrq, a, child);
         attr->val = ATTRVAL__MAX;          if (p->ncur == NODE_ENTITY && key == ATTRKEY_NAME)
         attr->rawval = NULL;                  xml_attrkey(p, "DEFINITION");
         TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child);  
         ps->attr = 1;  
 }  }
   
 static void  static void
 xml_attrval(struct parse *ps, const char *name)  xml_attrval(struct parse *p, const char *name)
 {  {
         struct pattr    *attr;          struct pattr    *a;
   
         if (ps->del > 0 || ps->attr == 0)          if (p->del > 0 || p->ncur >= NODE_UNKNOWN ||
               (p->flags & PFLAG_ATTR) == 0)
                 return;                  return;
         if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL)          if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL)
                 return;                  return;
         if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX &&          if ((a->val = attrval_parse(name)) == ATTRVAL__MAX &&
             (attr->rawval = strdup(name)) == NULL) {              (a->rawval = strdup(name)) == NULL)
                 perror(NULL);                  fatal(p);
                 exit(1);          p->flags &= ~PFLAG_ATTR;
         }  
 }  }
   
 /*  /*
Line 556  xml_attrval(struct parse *ps, const char *name)
Line 620  xml_attrval(struct parse *ps, const char *name)
  * If we're at a text node, roll that one up first.   * If we're at a text node, roll that one up first.
  */   */
 static void  static void
 xml_elem_end(struct parse *ps, const char *name)  xml_elem_end(struct parse *p, const char *name)
 {  {
         const struct element    *elem;          struct pnode            *n;
           const char              *cp;
         enum nodeid              node;          enum nodeid              node;
   
         /*          /*
          * An ancestor is excluded from the tree;           * An ancestor is excluded from the tree;
          * keep track of the number of levels excluded.           * keep track of the number of levels excluded.
          */           */
         if (ps->del > 1) {          if (p->del > 1) {
                 ps->del--;                  p->del--;
                 return;                  return;
         }          }
   
         if (ps->del == 0)          if (p->del == 0)
                 pnode_closetext(ps);                  pnode_closetext(p, 0);
   
         if (name != NULL) {          n = p->cur;
                 for (elem = elements; elem->name != NULL; elem++)          node = name == NULL ? p->ncur : xml_name2node(p, name);
                         if (strcmp(elem->name, name) == 0)  
                                 break;  
                 node = elem->node;  
         } else  
                 node = ps->ncur;  
   
         switch (node) {          switch (node) {
         case NODE_DELETE_WARN:          case NODE_DELETE_WARN:
         case NODE_DELETE:          case NODE_DELETE:
                 if (ps->del > 0)                  if (p->del > 0)
                         ps->del--;                          p->del--;
                 break;                  break;
         case NODE_IGNORE:          case NODE_IGNORE:
           case NODE_UNKNOWN:
                 break;                  break;
           case NODE_INCLUDE:
                   p->cur = n->parent;
                   cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL);
                   if (cp == NULL)
                           error_msg(p, "<xi:include> element "
                               "without href attribute");
                   else
                           parse_file(p, -1, cp);
                   pnode_unlink(n);
                   p->flags &= ~PFLAG_SPC;
                   break;
           case NODE_DOCTYPE:
           case NODE_SBR:
           case NODE_VOID:
                   p->flags &= ~PFLAG_EEND;
                   /* FALLTHROUGH */
         default:          default:
                 if (ps->cur == NULL || node != ps->cur->node) {                  if (n == NULL || node != n->node) {
                         warn_msg(ps, "element not open: </%s>", name);                          warn_msg(p, "element not open: </%s>", name);
                         break;                          break;
                 }                  }
                   if (pnode_class(node) == CLASS_NOFILL)
                           p->nofill--;
   
                 /*                  /*
                  * Refrain from actually closing the document element.                   * Refrain from actually closing the document element.
Line 602  xml_elem_end(struct parse *ps, const char *name)
Line 681  xml_elem_end(struct parse *ps, const char *name)
                  * obviously better than discarding it or crashing.                   * obviously better than discarding it or crashing.
                  */                   */
   
                 if (ps->cur->parent == NULL)                  if (n->parent != NULL || node == NODE_DOCTYPE) {
                         ps->tree->flags |= TREE_CLOSED;                          p->cur = n->parent;
                 else                          if (p->cur != NULL)
                         ps->cur = ps->cur->parent;                                  p->ncur = p->cur->node;
                 ps->spc = 0;                  } else
                           p->tree->flags |= TREE_CLOSED;
                   p->flags &= ~PFLAG_SPC;
   
                   /* Include a file containing entity declarations. */
   
                   if (node == NODE_ENTITY && strcmp("%",
                       pnode_getattr_raw(n, ATTRKEY_NAME, "")) == 0 &&
                       (cp = pnode_getattr_raw(n, ATTRKEY_SYSTEM, NULL)) != NULL)
                           parse_file(p, -1, cp);
   
                 break;                  break;
         }          }
         assert(ps->del == 0);          assert(p->del == 0);
 }  }
   
 struct parse *  struct parse *
Line 624  parse_alloc(int warn)
Line 713  parse_alloc(int warn)
                 free(p);                  free(p);
                 return NULL;                  return NULL;
         }          }
         p->warn = warn;          if (warn)
                   p->flags |= PFLAG_WARN;
           else
                   p->flags &= ~PFLAG_WARN;
         return p;          return p;
 }  }
   
Line 696  parse_string(struct parse *p, char *b, size_t rlen,
Line 788  parse_string(struct parse *p, char *b, size_t rlen,
     enum pstate *pstate, int refill)      enum pstate *pstate, int refill)
 {  {
         char            *cp;          char            *cp;
           size_t           pws;   /* Parse offset including whitespace. */
         size_t           poff;  /* Parse offset in b[]. */          size_t           poff;  /* Parse offset in b[]. */
         size_t           pend;  /* Offset of the end of the current word. */          size_t           pend;  /* Offset of the end of the current word. */
         int              elem_end;          int              elem_end;
   
         p->spc = 0;          pend = pws = 0;
         pend = 0;  
         for (;;) {          for (;;) {
   
                 /* Proceed to the next token, skipping whitespace. */                  /* Proceed to the next token, skipping whitespace. */
Line 713  parse_string(struct parse *p, char *b, size_t rlen,
Line 805  parse_string(struct parse *p, char *b, size_t rlen,
                 if ((poff = pend) == rlen)                  if ((poff = pend) == rlen)
                         break;                          break;
                 if (isspace((unsigned char)b[pend])) {                  if (isspace((unsigned char)b[pend])) {
                         p->spc = 1;                          p->flags |= PFLAG_SPC;
                           if (b[pend] == '\n')
                                   pws = pend + 1;
                         increment(p, b, &pend, refill);                          increment(p, b, &pend, refill);
                         continue;                          continue;
                 }                  }
Line 754  parse_string(struct parse *p, char *b, size_t rlen,
Line 848  parse_string(struct parse *p, char *b, size_t rlen,
                                         b[pend - 1] = '\0';                                          b[pend - 1] = '\0';
                                         elem_end = 1;                                          elem_end = 1;
                                 }                                  }
                                   if (p->flags & PFLAG_EEND)
                                           elem_end = 1;
                         }                          }
                         b[pend] = '\0';                          b[pend] = '\0';
                         if (pend < rlen)                          if (pend < rlen)
Line 765  parse_string(struct parse *p, char *b, size_t rlen,
Line 861  parse_string(struct parse *p, char *b, size_t rlen,
                 /* Look for an attribute name. */                  /* Look for an attribute name. */
   
                 } else if (*pstate == PARSE_TAG) {                  } else if (*pstate == PARSE_TAG) {
                           switch (p->ncur) {
                           case NODE_DOCTYPE:
                                   if (b[pend] == '[') {
                                           *pstate = PARSE_ELEM;
                                           increment(p, b, &pend, refill);
                                           continue;
                                   }
                                   /* FALLTHROUGH */
                           case NODE_ENTITY:
                                   if (b[pend] == '"' || b[pend] == '\'') {
                                           *pstate = PARSE_ARG;
                                           continue;
                                   }
                                   break;
                           default:
                                   break;
                           }
                         if (advance(p, b, rlen, &pend, " =>", refill))                          if (advance(p, b, rlen, &pend, " =>", refill))
                                 break;                                  break;
                         elem_end = 0;                          elem_end = 0;
Line 775  parse_string(struct parse *p, char *b, size_t rlen,
Line 888  parse_string(struct parse *p, char *b, size_t rlen,
                                         b[pend - 1] = '\0';                                          b[pend - 1] = '\0';
                                         elem_end = 1;                                          elem_end = 1;
                                 }                                  }
                                   if (p->flags & PFLAG_EEND)
                                           elem_end = 1;
                                 break;                                  break;
                         case '=':                          case '=':
                                 *pstate = PARSE_ARG;                                  *pstate = PARSE_ARG;
Line 823  parse_string(struct parse *p, char *b, size_t rlen,
Line 938  parse_string(struct parse *p, char *b, size_t rlen,
                         if (b[++poff] == '/') {                          if (b[++poff] == '/') {
                                 elem_end = 1;                                  elem_end = 1;
                                 poff++;                                  poff++;
                         } else                          } else {
                                 xml_elem_start(p, b + poff);                                  xml_elem_start(p, b + poff);
                                   if (*pstate == PARSE_ELEM &&
                                       p->flags & PFLAG_EEND)
                                           elem_end = 1;
                           }
                         if (elem_end)                          if (elem_end)
                                 xml_elem_end(p, b + poff);                                  xml_elem_end(p, b + poff);
   
                   /* Close a doctype. */
   
                   } else if (p->ncur == NODE_DOCTYPE && b[poff] == ']') {
                           *pstate = PARSE_TAG;
                           increment(p, b, &pend, refill);
   
                 /* Process an entity. */                  /* Process an entity. */
   
                 } else if (b[poff] == '&') {                  } else if (b[poff] == '&') {
Line 841  parse_string(struct parse *p, char *b, size_t rlen,
Line 966  parse_string(struct parse *p, char *b, size_t rlen,
                 /* Process text up to the next tag, entity, or EOL. */                  /* Process text up to the next tag, entity, or EOL. */
   
                 } else {                  } else {
                         advance(p, b, rlen, &pend, "<&", refill);                          advance(p, b, rlen, &pend,
                         xml_char(p, b + poff, pend - poff);                              p->ncur == NODE_DOCTYPE ? "<&]\n" : "<&\n",
                               refill);
                           if (p->nofill)
                                   poff = pws;
                           xml_text(p, b + poff, pend - poff);
                           if (b[pend] == '\n')
                                   pnode_closetext(p, 0);
                 }                  }
                   pws = pend;
         }          }
         return poff;          return poff;
 }  }
   
 struct ptree *  
 parse_file(struct parse *p, int fd, const char *fname)  /*
    * The read loop.
    * If the previous token was incomplete and asked for more input,
    * we have to enter the read loop once more even on EOF.
    * Once rsz is 0, incomplete tokens will no longer ask for more input
    * but instead use whatever there is, and then exit the read loop.
    * The minus one on the size limit for read(2) is needed such that
    * advance() can set b[rlen] to NUL when needed.
    */
   static void
   parse_fd(struct parse *p, int fd)
 {  {
         char             b[4096];          char             b[4096];
         ssize_t          rsz;   /* Return value from read(2). */          ssize_t          rsz;   /* Return value from read(2). */
Line 857  parse_file(struct parse *p, int fd, const char *fname)
Line 999  parse_file(struct parse *p, int fd, const char *fname)
         size_t           poff;  /* Parse offset in b[]. */          size_t           poff;  /* Parse offset in b[]. */
         enum pstate      pstate;          enum pstate      pstate;
   
         p->fname = fname;  
         p->nline = 1;  
         p->ncol = 1;  
         pstate = PARSE_ELEM;  
         rlen = 0;          rlen = 0;
           pstate = PARSE_ELEM;
         /*  
          * Read loop.  
          *  
          * If the previous token was incomplete and asked for more  
          * input, we have to enter the read loop once more even on EOF.  
          * Once rsz is 0, incomplete tokens will no longer ask  
          * for more input but instead use whatever there is,  
          * and then exit the read loop.  
          * The minus one on the size limit for read(2) is needed  
          * such that advance() can set b[rlen] to NUL when needed.  
          */  
   
         while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&          while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
             (rlen += rsz) > 0) {              (rlen += rsz) > 0) {
                 poff = parse_string(p, b, rlen, &pstate, rsz > 0);                  poff = parse_string(p, b, rlen, &pstate, rsz > 0);
Line 883  parse_file(struct parse *p, int fd, const char *fname)
Line 1009  parse_file(struct parse *p, int fd, const char *fname)
                 rlen -= poff;                  rlen -= poff;
                 memmove(b, b + poff, rlen);                  memmove(b, b + poff, rlen);
         }          }
         if (rsz < 0) {          if (rsz < 0)
                 perror(fname);                  error_msg(p, "read: %s", strerror(errno));
                 p->tree->flags |= TREE_FAIL;  }
   
   /*
    * Open and parse a file.
    */
   struct ptree *
   parse_file(struct parse *p, int fd, const char *fname)
   {
           const char      *save_fname;
           int              save_line, save_col;
   
           /* Save and initialize reporting data. */
   
           save_fname = p->fname;
           save_line = p->nline;
           save_col = p->ncol;
           p->fname = fname;
           p->line = 0;
           p->col = 0;
   
           /* Open the file, unless it is already open. */
   
           if (fd == -1 && (fd = open(fname, O_RDONLY, 0)) == -1) {
                   error_msg(p, "open: %s", strerror(errno));
                   p->fname = save_fname;
                   return p->tree;
         }          }
         pnode_closetext(p);  
         if ((p->tree->flags & TREE_CLOSED) == 0)          /*
                 warn_msg(p, "document not closed");           * After opening the starting file, change to the directory it
            * is located in, in case it wants to include any further files,
            * which are typically given with relative paths in DocBook.
            * Do this on a best-effort basis; don't complain about failure.
            */
   
           if (save_fname == NULL && (fname = dirname(fname)) != NULL &&
               strcmp(fname, ".") != 0)
                   (void)chdir(fname);
   
           /* Run the read loop. */
   
           p->nline = 1;
           p->ncol = 1;
           parse_fd(p, fd);
   
           /* On the top level, finalize the parse tree. */
   
           if (save_fname == NULL) {
                   pnode_closetext(p, 0);
                   if (p->tree->root == NULL)
                           error_msg(p, "empty document");
                   else if ((p->tree->flags & TREE_CLOSED) == 0)
                           warn_msg(p, "document not closed");
                   pnode_unlink(p->doctype);
           }
   
           /* Clean up. */
   
           if (fd != STDIN_FILENO)
                   close(fd);
           p->fname = save_fname;
           p->nline = save_line;
           p->ncol = save_col;
         return p->tree;          return p->tree;
 }  }

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.50

CVSweb