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

Diff for /docbook2mdoc/parse.c between version 1.4 and 1.5

version 1.4, 2019/03/26 22:39:33 version 1.5, 2019/03/28 12:21:10
Line 17 
Line 17 
  */   */
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
 #include <expat.h>  
 #include <stdio.h>  #include <stdio.h>
   #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
Line 34 
Line 34 
  * Keep this as simple and small as possible.   * Keep this as simple and small as possible.
  */   */
 struct  parse {  struct  parse {
         XML_Parser       xml;  
         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    *cur;    /* Current node in the tree. */          struct pnode    *cur;    /* Current node in the tree. */
           enum nodeid      ncur;   /* Type of the current node. */
           int              line;   /* Line number in the input file. */
           int              col;    /* Column number in the input file. */
           int              nline;  /* Line 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              attr;   /* The most recent attribute is valid. */
         int              warn;          int              warn;
 };  };
   
Line 179  static const struct element elements[] = {
Line 184  static const struct element elements[] = {
         { "wordasword",         NODE_WORDASWORD },          { "wordasword",         NODE_WORDASWORD },
         { "xi:include",         NODE_DELETE_WARN },          { "xi:include",         NODE_DELETE_WARN },
         { "year",               NODE_YEAR },          { "year",               NODE_YEAR },
         { NULL,                 NODE__MAX }          { NULL,                 NODE_IGNORE }
 };  };
   
 /*  /*
Line 188  static const struct element elements[] = {
Line 193  static const struct element elements[] = {
  * 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(void *arg, const XML_Char *p, int sz)  xml_char(struct parse *ps, const char *p, int sz)
 {  {
         struct parse    *ps;  
         struct pnode    *dat;          struct pnode    *dat;
         int              i;  
   
         ps = arg;          if (ps->del > 0)
         if (ps->del > 0 || ps->tree->flags & TREE_FAIL)  
                 return;                  return;
   
         /*          if (ps->cur == NULL) {
          * Only create a new node if there is non-whitespace text.                  fprintf(stderr, "%s:%d:%d: discarding text before docum"
          * Strip all leading whitespace.                      "ent: %.*s\n", ps->fname, ps->line, ps->col, sz, p);
          */                  ps->tree->flags |= TREE_FAIL;
         if (ps->cur->node != NODE_TEXT) {                  return;
                 for (i = 0; i < sz; i++)          }
                         if (isspace((unsigned char)p[i]) == 0)  
                                 break;  
                 if (i == sz)  
                         return;  
                 p += i;  
                 sz -= i;  
   
           if (ps->cur->node != NODE_TEXT) {
                 if ((dat = calloc(1, sizeof(*dat))) == NULL) {                  if ((dat = calloc(1, sizeof(*dat))) == NULL) {
                         perror(NULL);                          perror(NULL);
                         exit(1);                          exit(1);
Line 223  xml_char(void *arg, const XML_Char *p, int sz)
Line 220  xml_char(void *arg, const XML_Char *p, int sz)
                 ps->cur = dat;                  ps->cur = dat;
         }          }
   
           if (ps->tree->flags & TREE_CLOSED &&
               ps->cur->parent == ps->tree->root && ps->warn)
                   fprintf(stderr, "%s:%d:%d: warning: "
                       "text after end of document: %.*s\n",
                       ps->fname, ps->line, ps->col, sz, p);
   
         /* Append to the current text node. */          /* Append to the current text node. */
   
         assert(sz >= 0);          assert(sz >= 0);
Line 248  pnode_trim(struct pnode *pn)
Line 251  pnode_trim(struct pnode *pn)
   
 /*  /*
  * Begin an element.   * Begin an element.
  * If the name is unknown, abort parsing.  
  */   */
 static void  static void
 xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts)  xml_elem_start(struct parse *ps, const char *name)
 {  {
         struct parse     *ps;          const struct element    *elem;
         const struct element *elem;          struct pnode            *dat;
         enum attrkey      key;  
         struct pnode     *dat;  
         struct pattr     *pattr;  
         const XML_Char  **att;  
   
         ps = arg;          if (*name == '!' || *name == '?')
         if (ps->tree->flags & TREE_FAIL)  
                 return;                  return;
   
         /*          /*
Line 284  xml_elem_start(void *arg, const XML_Char *name, const 
Line 281  xml_elem_start(void *arg, const XML_Char *name, const 
                         break;                          break;
   
         if (elem->name == NULL) {          if (elem->name == NULL) {
                 fprintf(stderr, "%s:%zu:%zu: unknown element \"%s\"\n",                  fprintf(stderr, "%s:%d:%d: unknown element <%s>\n",
                         ps->fname, XML_GetCurrentLineNumber(ps->xml),                          ps->fname, ps->line, ps->col, name);
                         XML_GetCurrentColumnNumber(ps->xml), name);  
                 ps->tree->flags |= TREE_FAIL;                  ps->tree->flags |= TREE_FAIL;
                 return;  
         }          }
           ps->ncur = elem->node;
   
         switch (elem->node) {          switch (ps->ncur) {
         case NODE_DELETE_WARN:          case NODE_DELETE_WARN:
                 if (ps->warn)                  if (ps->warn)
                         fprintf(stderr, "%s:%zu:%zu: warning: "                          fprintf(stderr, "%s:%d:%d: warning: "
                             "skipping element <%s>\n", ps->fname,                              "skipping element <%s>\n",
                             XML_GetCurrentLineNumber(ps->xml),                              ps->fname, ps->line, ps->col, name);
                             XML_GetCurrentColumnNumber(ps->xml), name);  
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case NODE_DELETE:          case NODE_DELETE:
                 ps->del = 1;                  ps->del = 1;
Line 311  xml_elem_start(void *arg, const XML_Char *name, const 
Line 306  xml_elem_start(void *arg, const XML_Char *name, const 
                 break;                  break;
         }          }
   
           if (ps->tree->flags & TREE_CLOSED &&
               ps->cur->parent == NULL && ps->warn)
                   fprintf(stderr, "%s:%d:%d: warning: "
                       "element after end of document: %s\n",
                       ps->fname, ps->line, ps->col, name);
   
         if ((dat = calloc(1, sizeof(*dat))) == NULL) {          if ((dat = calloc(1, sizeof(*dat))) == NULL) {
                 perror(NULL);                  perror(NULL);
                 exit(1);                  exit(1);
Line 326  xml_elem_start(void *arg, const XML_Char *name, const 
Line 327  xml_elem_start(void *arg, const XML_Char *name, const 
         ps->cur = dat;          ps->cur = dat;
         if (ps->tree->root == NULL)          if (ps->tree->root == NULL)
                 ps->tree->root = dat;                  ps->tree->root = dat;
   }
   
         /*  static void
          * Process attributes.  xml_attrkey(struct parse *ps, const char *name)
          */  {
         for (att = atts; *att != NULL; att += 2) {          struct pattr    *attr;
                 if ((key = attrkey_parse(*att)) == ATTRKEY__MAX) {          enum attrkey     key;
                         if (ps->warn)  
                                 fprintf(stderr, "%s:%zu:%zu: warning: "          if (ps->del > 0 || *name == '\0')
                                     "unknown attribute \"%s\"\n",                  return;
                                     ps->fname,          if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
                                     XML_GetCurrentLineNumber(ps->xml),                  if (ps->warn)
                                     XML_GetCurrentColumnNumber(ps->xml),                          fprintf(stderr, "%s:%d:%d: warning: "
                                     *att);                              "unknown attribute \"%s\"\n",
                         continue;                              ps->fname, ps->line, ps->col, name);
                 }                  ps->attr = 0;
                 pattr = calloc(1, sizeof(*pattr));                  return;
                 pattr->key = key;  
                 if ((pattr->val = attrval_parse(att[1])) == ATTRVAL__MAX)  
                         pattr->rawval = strdup(att[1]);  
                 TAILQ_INSERT_TAIL(&dat->attrq, pattr, child);  
         }          }
           if ((attr = calloc(1, sizeof(*attr))) == NULL) {
                   perror(NULL);
                   exit(1);
           }
           attr->key = key;
           attr->val = ATTRVAL__MAX;
           attr->rawval = NULL;
           TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child);
           ps->attr = 1;
 }  }
   
   static void
   xml_attrval(struct parse *ps, const char *name)
   {
           struct pattr    *attr;
   
           if (ps->del > 0 || ps->attr == 0)
                   return;
           if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL)
                   return;
           if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX &&
               (attr->rawval = strdup(name)) == NULL) {
                   perror(NULL);
                   exit(1);
           }
   }
   
 /*  /*
  * Roll up the parse tree.   * Roll up the parse tree.
  * 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(void *arg, const XML_Char *name)  xml_elem_end(struct parse *ps, const char *name)
 {  {
         struct parse    *ps;          const struct element    *elem;
         const struct element *elem;          enum nodeid              node;
   
         ps = arg;  
         if (ps->tree->flags & TREE_FAIL)  
                 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.
Line 373  xml_elem_end(void *arg, const XML_Char *name)
Line 392  xml_elem_end(void *arg, const XML_Char *name)
         }          }
   
         /* Close out the text node, if there is one. */          /* Close out the text node, if there is one. */
         if (ps->del == 0 && ps->cur->node == NODE_TEXT) {          if (ps->del == 0 && ps->cur != NULL && ps->cur->node == NODE_TEXT) {
                 pnode_trim(ps->cur);                  pnode_trim(ps->cur);
                 ps->cur = ps->cur->parent;                  ps->cur = ps->cur->parent;
         }          }
   
         for (elem = elements; elem->name != NULL; elem++)          if (name != NULL) {
                 if (strcmp(elem->name, name) == 0)                  for (elem = elements; elem->name != NULL; elem++)
                         break;                          if (strcmp(elem->name, name) == 0)
                                   break;
                   node = elem->node;
           } else
                   node = ps->ncur;
   
         switch (elem->node) {          switch (node) {
         case NODE_DELETE_WARN:          case NODE_DELETE_WARN:
         case NODE_DELETE:          case NODE_DELETE:
                 ps->del--;                  if (ps->del > 0)
                           ps->del--;
                 break;                  break;
         case NODE_IGNORE:          case NODE_IGNORE:
                 break;                  break;
         default:          default:
                 assert(elem->node == ps->cur->node);                  if (ps->cur == NULL || node != ps->cur->node) {
                 ps->cur = ps->cur->parent;                          if (ps->warn)
                                   fprintf(stderr, "%s:%d:%d: warning: "
                                       "element not open: </%s>\n",
                                       ps->fname, ps->line, ps->col, name);
                           break;
                   }
   
                   /*
                    * Refrain from actually closing the document element.
                    * If no more content follows, no harm is done, but if
                    * some content still follows, simply processing it is
                    * obviously better than discarding it or crashing.
                    */
   
                   if (ps->cur->parent == NULL)
                           ps->tree->flags |= TREE_CLOSED;
                   else
                           ps->cur = ps->cur->parent;
                 break;                  break;
         }          }
         assert(ps->del == 0);          assert(ps->del == 0);
Line 409  parse_alloc(int warn)
Line 450  parse_alloc(int warn)
                 free(p);                  free(p);
                 return NULL;                  return NULL;
         }          }
   
         if ((p->xml = XML_ParserCreate(NULL)) == NULL) {  
                 free(p->tree);  
                 free(p);  
                 return NULL;  
         }  
         p->warn = warn;          p->warn = warn;
         XML_SetCharacterDataHandler(p->xml, xml_char);  
         XML_SetElementHandler(p->xml, xml_elem_start, xml_elem_end);  
         XML_SetUserData(p->xml, p);  
         return p;          return p;
 }  }
   
Line 427  parse_free(struct parse *p)
Line 459  parse_free(struct parse *p)
 {  {
         if (p == NULL)          if (p == NULL)
                 return;                  return;
         XML_ParserFree(p->xml);  
         if (p->tree != NULL) {          if (p->tree != NULL) {
                 pnode_unlink(p->tree->root);                  pnode_unlink(p->tree->root);
                 free(p->tree);                  free(p->tree);
Line 435  parse_free(struct parse *p)
Line 466  parse_free(struct parse *p)
         free(p);          free(p);
 }  }
   
   /*
    * Advance the pend pointer to the next character in the charset.
    * If the charset starts with a space, it stands for any whitespace.
    * Update the new input file position, used for messages.
    * Do not overrun the buffer b of length rlen.
    * When reaching the end, NUL-terminate the buffer and return 1;
    * otherwise, return 0.
    */
   static int
   advance(struct parse *p, char *b, size_t rlen, size_t *pend,
       const char *charset)
   {
           int              space;
   
           if (*charset == ' ') {
                   space = 1;
                   charset++;
           } else
                   space = 0;
   
           p->nline = p->line;
           p->ncol = p->col;
           while (*pend < rlen) {
                   if (b[*pend] == '\n') {
                           p->nline++;
                           p->ncol = 1;
                   } else
                           p->ncol++;
                   if (space && isspace((unsigned char)b[*pend]))
                           break;
                   if (strchr(charset, b[*pend]) != NULL)
                           break;
                   ++*pend;
           }
           if (*pend == rlen) {
                   b[rlen] = '\0';
                   return 1;
           } else
                   return 0;
   }
   
 struct ptree *  struct ptree *
 parse_file(struct parse *p, int fd, const char *fname)  parse_file(struct parse *p, int fd, const char *fname)
 {  {
         char             b[4096];          char             b[4096];
         ssize_t          ssz;          ssize_t          rsz;   /* Return value from read(2). */
           size_t           rlen;  /* Number of bytes in b[]. */
           size_t           poff;  /* Parse offset in b[]. */
           size_t           pend;  /* Offset of the end of the current word. */
           int              in_tag, in_arg, in_quotes, elem_end;
   
         p->fname = fname;          p->fname = fname;
         do {          p->nline = 1;
                 if ((ssz = read(fd, b, sizeof(b))) < 0) {          p->ncol = 1;
                         perror(fname);          rlen = 0;
                         pnode_unlink(p->tree->root);          in_tag = in_arg = in_quotes = 0;
                         p->tree->root = p->cur = NULL;  
                         p->tree->flags |= TREE_FAIL;          /*
                         return NULL;           * Read loop.
            *
            * We have to enter the read loop once more even on EOF
            * because the previous token may have been incomplete,
            * such that it asked for more input.
            * 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) {
                   if ((rlen += rsz) == 0)
                           break;
   
                   /* Token loop. */
   
                   pend = 0;
                   for (;;) {
   
                           /* Proceed to the next token, skipping whitespace. */
   
                           p->line = p->nline;
                           p->col = p->ncol;
                           if ((poff = pend) == rlen)
                                   break;
                           if (isspace((unsigned char)b[pend])) {
                                   if (b[pend++] == '\n') {
                                           p->nline++;
                                           p->ncol = 1;
                                   } else
                                           p->ncol++;
                                   continue;
                           }
   
                           /*
                            * The following three cases (in_arg, in_tag,
                            * and starting a tag) all parse a word or
                            * quoted string.  If that extends beyond the
                            * read buffer and the last read(2) still got
                            * data, they all break out of the token loop
                            * to request more data from the read loop.
                            *
                            * Also, they all detect self-closing tags,
                            * those ending with "/>", setting the flag
                            * elem_end and calling xml_elem_end() at the
                            * very end, after handling the attribute value,
                            * attribute name, or tag name, respectively.
                            */
   
                           /* Parse an attribute value. */
   
                           if (in_arg) {
                                   if (in_quotes == 0 && b[pend] == '"') {
                                           in_quotes = 1;
                                           p->ncol++;
                                           pend++;
                                           continue;
                                   }
                                   if (advance(p, b, rlen, &pend,
                                       in_quotes ? "\"" : " >") && rsz > 0)
                                           break;
                                   in_arg = in_quotes = elem_end = 0;
                                   if (b[pend] == '>') {
                                           in_tag = 0;
                                           if (pend > 0 && b[pend - 1] == '/') {
                                                   b[pend - 1] = '\0';
                                                   elem_end = 1;
                                           }
                                   }
                                   b[pend] = '\0';
                                   if (pend < rlen)
                                           pend++;
                                   xml_attrval(p, b + poff);
                                   if (elem_end)
                                           xml_elem_end(p, NULL);
   
                           /* Look for an attribute name. */
   
                           } else if (in_tag) {
                                   if (advance(p, b, rlen, &pend, " =>") &&
                                       rsz > 0)
                                           break;
                                   elem_end = 0;
                                   switch (b[pend]) {
                                   case '>':
                                           in_tag = 0;
                                           if (pend > 0 && b[pend - 1] == '/') {
                                                   b[pend - 1] = '\0';
                                                   elem_end = 1;
                                           }
                                           break;
                                   case '=':
                                           in_arg = 1;
                                           break;
                                   default:
                                           break;
                                   }
                                   b[pend] = '\0';
                                   if (pend < rlen)
                                           pend++;
                                   xml_attrkey(p, b + poff);
                                   if (elem_end)
                                           xml_elem_end(p, NULL);
   
                           /* Begin an opening or closing tag. */
   
                           } else if (b[poff] == '<') {
                                   if (advance(p, b, rlen, &pend, " >") &&
                                       rsz > 0)
                                           break;
                                   elem_end = 0;
                                   if (b[pend] != '>')
                                           in_tag = 1;
                                   else if (pend > 0 && b[pend - 1] == '/') {
                                           b[pend - 1] = '\0';
                                           elem_end = 1;
                                   }
                                   b[pend] = '\0';
                                   if (pend < rlen)
                                           pend++;
                                   if (b[++poff] == '/') {
                                           elem_end = 1;
                                           poff++;
                                   } else
                                           xml_elem_start(p, b + poff);
                                   if (elem_end)
                                           xml_elem_end(p, b + poff);
   
                           /* Process text up to the next tag. */
   
                           } else {
                                   if (advance(p, b, rlen, &pend, "<") == 0)
                                           p->ncol--;
                                   xml_char(p, b + poff, pend - poff);
                           }
                 }                  }
                 if (XML_Parse(p->xml, b, ssz, ssz == 0) == 0) {  
                         fprintf(stderr, "%s:%zu:%zu: %s\n", fname,                  /* Buffer exhausted; shift left and re-fill. */
                             XML_GetCurrentLineNumber(p->xml),  
                             XML_GetCurrentColumnNumber(p->xml),                  assert(poff > 0);
                             XML_ErrorString(XML_GetErrorCode(p->xml)));                  memmove(b, b + poff, rlen - poff);
                         p->tree->flags |= TREE_FAIL;                  rlen -= poff;
                 }          }
         } while (ssz > 0 && (p->tree->flags & TREE_FAIL) == 0);          if (rsz < 0) {
                   perror(fname);
                   p->tree->flags |= TREE_FAIL;
           }
           if (p->cur != NULL && p->cur->node == NODE_TEXT) {
                   pnode_trim(p->cur);
                   p->cur = p->cur->parent;
           }
           if ((p->tree->flags & TREE_CLOSED) == 0 && p->warn)
                   fprintf(stderr, "%s:%d:%d: warning: document not closed\n",
                       p->fname, p->line, p->col);
         return p->tree;          return p->tree;
 }  }

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

CVSweb