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

Diff for /docbook2mdoc/parse.c between version 1.30 and 1.34

version 1.30, 2019/04/10 14:22:37 version 1.34, 2019/04/12 04:39:24
Line 325  static void
Line 325  static void
 xml_char(struct parse *p, const char *word, int sz)  xml_char(struct parse *p, const char *word, int sz)
 {  {
         struct pnode    *n;          struct pnode    *n;
         size_t           newsz;          size_t           oldsz, newsz;
   
           assert(sz > 0);
         if (p->del > 0)          if (p->del > 0)
                 return;                  return;
   
         if (p->cur == NULL) {          if ((n = p->cur) == NULL) {
                 error_msg(p, "discarding text before document: %.*s", sz, word);                  error_msg(p, "discarding text before document: %.*s", sz, word);
                 return;                  return;
         }          }
   
         if (p->cur->node != NODE_TEXT) {          if (n->node != NODE_TEXT) {
                 if ((n = calloc(1, sizeof(*n))) == NULL)                  if ((n = pnode_alloc(p->cur)) == NULL)
                         fatal(p);                          fatal(p);
                 n->node = NODE_TEXT;                  n->node = NODE_TEXT;
                 n->spc = (p->flags & PFLAG_SPC) != 0;                  n->spc = (p->flags & PFLAG_SPC) != 0;
                 n->parent = p->cur;  
                 TAILQ_INIT(&n->childq);  
                 TAILQ_INIT(&n->attrq);  
                 TAILQ_INSERT_TAIL(&p->cur->childq, n, child);  
                 p->cur = n;                  p->cur = n;
         }          }
   
         if (p->tree->flags & TREE_CLOSED &&          if (p->tree->flags & TREE_CLOSED && n->parent == p->tree->root)
             p->cur->parent == p->tree->root)  
                 warn_msg(p, "text after end of document: %.*s", sz, word);                  warn_msg(p, "text after end of document: %.*s", sz, word);
   
         /* Append to the current text node. */          /* Append to the current text node. */
   
         assert(sz >= 0);          oldsz = n->b == NULL ? 0 : strlen(n->b);
         newsz = p->cur->bsz + (p->cur->bsz && (p->flags & PFLAG_SPC)) + sz;          newsz = oldsz + sz;
         if ((p->cur->b = realloc(p->cur->b, newsz + 1)) == NULL)          if (oldsz && (p->flags & PFLAG_SPC))
                   newsz++;
           if ((n->b = realloc(n->b, newsz + 1)) == NULL)
                 fatal(p);                  fatal(p);
         if (p->cur->bsz && (p->flags & PFLAG_SPC))          if (oldsz && (p->flags & PFLAG_SPC))
                 p->cur->b[p->cur->bsz++] = ' ';                  n->b[oldsz++] = ' ';
         memcpy(p->cur->b + p->cur->bsz, word, sz);          memcpy(n->b + oldsz, word, sz);
         p->cur->b[p->cur->bsz = newsz] = '\0';          n->b[newsz] = '\0';
         p->cur->real = p->cur->b;  
         p->flags &= ~PFLAG_SPC;          p->flags &= ~PFLAG_SPC;
 }  }
   
Line 372  static void
Line 369  static void
 pnode_closetext(struct parse *p)  pnode_closetext(struct parse *p)
 {  {
         struct pnode    *n;          struct pnode    *n;
           char            *cp;
   
         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]);
               *--cp = '\0')
                 p->flags |= PFLAG_SPC;                  p->flags |= PFLAG_SPC;
         }  
 }  }
   
 static void  static void
Line 438  xml_entity(struct parse *p, const char *name)
Line 436  xml_entity(struct parse *p, const char *name)
         }          }
   
         /* Create, append, and close out an entity node. */          /* Create, append, and close out an entity node. */
         if ((n = calloc(1, sizeof(*n))) == NULL ||          if ((n = pnode_alloc(p->cur)) == NULL ||
             (n->b = n->real = strdup(entity->roff)) == NULL)              (n->b = strdup(entity->roff)) == NULL)
                 fatal(p);                  fatal(p);
         n->node = NODE_ESCAPE;          n->node = NODE_ESCAPE;
         n->bsz = strlen(n->b);  
         n->spc = (p->flags & PFLAG_SPC) != 0;          n->spc = (p->flags & PFLAG_SPC) != 0;
         n->parent = p->cur;  
         TAILQ_INIT(&n->childq);  
         TAILQ_INIT(&n->attrq);  
         TAILQ_INSERT_TAIL(&p->cur->childq, n, child);  
         p->flags &= ~PFLAG_SPC;          p->flags &= ~PFLAG_SPC;
 }  }
   
Line 503  xml_elem_start(struct parse *p, const char *name)
Line 496  xml_elem_start(struct parse *p, const char *name)
         if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL)          if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL)
                 warn_msg(p, "element after end of document: <%s>", name);                  warn_msg(p, "element after end of document: <%s>", name);
   
         if ((n = calloc(1, sizeof(*n))) == NULL)          if ((n = pnode_alloc(p->cur)) == NULL)
                 fatal(p);                  fatal(p);
   
         /*          /*
Line 555  xml_elem_start(struct parse *p, const char *name)
Line 548  xml_elem_start(struct parse *p, const char *name)
                 n->spc = (p->flags & PFLAG_SPC) != 0;                  n->spc = (p->flags & PFLAG_SPC) != 0;
                 break;                  break;
         }          }
         n->parent = p->cur;  
         TAILQ_INIT(&n->childq);  
         TAILQ_INIT(&n->attrq);  
   
         if (p->cur != NULL)  
                 TAILQ_INSERT_TAIL(&p->cur->childq, n, child);  
   
         p->cur = n;          p->cur = n;
         if (n->node == NODE_DOCTYPE) {          if (n->node == NODE_DOCTYPE) {
                 if (p->doctype == NULL)                  if (p->doctype == NULL)
Line 632  xml_attrval(struct parse *p, const char *name)
Line 618  xml_attrval(struct parse *p, 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;          const struct element    *elem;
         struct pnode            *n;          struct pnode            *n;
Line 643  xml_elem_end(struct parse *ps, const char *name)
Line 629  xml_elem_end(struct parse *ps, const char *name)
          * 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);
   
         if (name != NULL) {          if (name != NULL) {
                 for (elem = elements; elem->name != NULL; elem++)                  for (elem = elements; elem->name != NULL; elem++)
Line 657  xml_elem_end(struct parse *ps, const char *name)
Line 643  xml_elem_end(struct parse *ps, const char *name)
                                 break;                                  break;
                 node = elem->node;                  node = elem->node;
         } else          } else
                 node = ps->ncur;                  node = p->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:
                 break;                  break;
         case NODE_INCLUDE:          case NODE_INCLUDE:
                 n = ps->cur;                  n = p->cur;
                 ps->cur = ps->cur->parent;                  p->cur = p->cur->parent;
                 cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL);                  cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL);
                 if (cp == NULL)                  if (cp == NULL)
                         error_msg(ps, "<xi:include> element "                          error_msg(p, "<xi:include> element "
                             "without href attribute");                              "without href attribute");
                 else                  else
                         parse_file(ps, -1, cp);                          parse_file(p, -1, cp);
                 pnode_unlink(n);                  pnode_unlink(n);
                 ps->flags &= ~PFLAG_SPC;                  p->flags &= ~PFLAG_SPC;
                 break;                  break;
         case NODE_DOCTYPE:          case NODE_DOCTYPE:
                 ps->flags &= ~PFLAG_EEND;          case NODE_SBR:
                   p->flags &= ~PFLAG_EEND;
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         default:          default:
                 if (ps->cur == NULL || node != ps->cur->node) {                  if (p->cur == NULL || node != p->cur->node) {
                         warn_msg(ps, "element not open: </%s>", name);                          warn_msg(p, "element not open: </%s>", name);
                         break;                          break;
                 }                  }
   
Line 695  xml_elem_end(struct parse *ps, const char *name)
Line 682  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 || node == NODE_DOCTYPE) {                  if (p->cur->parent != NULL || node == NODE_DOCTYPE) {
                         ps->cur = ps->cur->parent;                          p->cur = p->cur->parent;
                         if (ps->cur != NULL)                          if (p->cur != NULL)
                                 ps->ncur = ps->cur->node;                                  p->ncur = p->cur->node;
                 } else                  } else
                         ps->tree->flags |= TREE_CLOSED;                          p->tree->flags |= TREE_CLOSED;
                 ps->flags &= ~PFLAG_SPC;                  p->flags &= ~PFLAG_SPC;
                 break;                  break;
         }          }
         assert(ps->del == 0);          assert(p->del == 0);
 }  }
   
 struct parse *  struct parse *
Line 970  parse_string(struct parse *p, char *b, size_t rlen,
Line 957  parse_string(struct parse *p, char *b, size_t rlen,
   
                 } else {                  } else {
                         advance(p, b, rlen, &pend,                          advance(p, b, rlen, &pend,
                             p->ncur == NODE_DOCTYPE ? "<&]" : "<&",                              p->ncur == NODE_DOCTYPE ? "<&]\n" : "<&\n",
                             refill);                              refill);
                         xml_char(p, b + poff, pend - poff);                          xml_char(p, b + poff, pend - poff);
                           if (b[pend] == '\n')
                                   pnode_closetext(p);
                 }                  }
         }          }
         return poff;          return poff;

Legend:
Removed from v.1.30  
changed lines
  Added in v.1.34

CVSweb