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

Diff for /docbook2mdoc/parse.c between version 1.15 and 1.16

version 1.15, 2019/04/06 13:45:58 version 1.16, 2019/04/06 22:37:57
Line 52  struct parse {
Line 52  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              attr;   /* The most recent attribute is valid. */          int              attr;   /* The most recent attribute is valid. */
         int              warn;          int              warn;
 };  };
Line 294  static void
Line 295  static void
 xml_char(struct parse *ps, const char *p, int sz)  xml_char(struct parse *ps, const char *p, int sz)
 {  {
         struct pnode    *dat;          struct pnode    *dat;
           size_t           newsz;
   
         if (ps->del > 0)          if (ps->del > 0)
                 return;                  return;
Line 309  xml_char(struct parse *ps, const char *p, int sz)
Line 311  xml_char(struct parse *ps, const char *p, int sz)
                         exit(1);                          exit(1);
                 }                  }
                 dat->node = NODE_TEXT;                  dat->node = NODE_TEXT;
                   dat->spc = ps->spc;
                 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 323  xml_char(struct parse *ps, const char *p, int sz)
Line 326  xml_char(struct parse *ps, const char *p, int sz)
         /* Append to the current text node. */          /* Append to the current text node. */
   
         assert(sz >= 0);          assert(sz >= 0);
         ps->cur->b = realloc(ps->cur->b, ps->cur->bsz + sz + 1);          newsz = ps->cur->bsz + (ps->cur->bsz && ps->spc) + sz;
           ps->cur->b = realloc(ps->cur->b, newsz + 1);
         if (ps->cur->b == NULL) {          if (ps->cur->b == NULL) {
                 perror(NULL);                  perror(NULL);
                 exit(1);                  exit(1);
         }          }
           if (ps->cur->bsz && ps->spc)
                   ps->cur->b[ps->cur->bsz++] = ' ';
         memcpy(ps->cur->b + ps->cur->bsz, p, sz);          memcpy(ps->cur->b + ps->cur->bsz, p, sz);
         ps->cur->bsz += sz;          ps->cur->b[ps->cur->bsz = newsz] = '\0';
         ps->cur->b[ps->cur->bsz] = '\0';  
         ps->cur->real = ps->cur->b;          ps->cur->real = ps->cur->b;
           ps->spc = 0;
 }  }
   
   /*
    * Close out the text node and strip trailing whitespace, if one is open.
    */
 static void  static void
 pnode_trim(struct pnode *pn)  pnode_closetext(struct parse *p)
 {  {
         assert(pn->node == NODE_TEXT);          struct pnode    *n;
         for (; pn->bsz > 0; pn->b[--pn->bsz] = '\0')  
                 if (isspace((unsigned char)pn->b[pn->bsz - 1]) == 0)          if ((n = p->cur) == NULL || n->node != NODE_TEXT)
                         break;                  return;
           p->cur = n->parent;
           while (n->bsz > 0 && isspace((unsigned char)n->b[n->bsz - 1])) {
                   n->b[--n->bsz] = '\0';
                   p->spc = 1;
           }
 }  }
   
 static void  static void
Line 357  xml_entity(struct parse *p, const char *name)
Line 371  xml_entity(struct parse *p, const char *name)
                 return;                  return;
         }          }
   
         /* Close out the text node, if there is one. */          pnode_closetext(p);
         if (p->cur->node == NODE_TEXT) {  
                 pnode_trim(p->cur);  
                 p->cur = p->cur->parent;  
         }  
   
         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 383  xml_entity(struct parse *p, const char *name)
Line 393  xml_entity(struct parse *p, const char *name)
         }          }
         dat->node = NODE_ESCAPE;          dat->node = NODE_ESCAPE;
         dat->bsz = strlen(dat->b);          dat->bsz = strlen(dat->b);
           dat->spc = p->spc;
         dat->parent = p->cur;          dat->parent = p->cur;
         TAILQ_INIT(&dat->childq);          TAILQ_INIT(&dat->childq);
         TAILQ_INIT(&dat->attrq);          TAILQ_INIT(&dat->attrq);
         TAILQ_INSERT_TAIL(&p->cur->childq, dat, child);          TAILQ_INSERT_TAIL(&p->cur->childq, dat, child);
           p->spc = 0;
 }  }
   
 /*  /*
Line 410  xml_elem_start(struct parse *ps, const char *name)
Line 422  xml_elem_start(struct parse *ps, const char *name)
                 return;                  return;
         }          }
   
         /* Close out the text node, if there is one. */          pnode_closetext(ps);
         if (ps->cur != NULL && ps->cur->node == NODE_TEXT) {  
                 pnode_trim(ps->cur);  
                 ps->cur = ps->cur->parent;  
         }  
   
         for (elem = elements; elem->name != NULL; elem++)          for (elem = elements; elem->name != NULL; elem++)
                 if (strcmp(elem->name, name) == 0)                  if (strcmp(elem->name, name) == 0)
Line 449  xml_elem_start(struct parse *ps, const char *name)
Line 457  xml_elem_start(struct parse *ps, const char *name)
                 exit(1);                  exit(1);
         }          }
         dat->node = elem->node;          dat->node = elem->node;
           dat->spc = ps->spc;
         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 519  xml_elem_end(struct parse *ps, const char *name)
Line 528  xml_elem_end(struct parse *ps, const char *name)
                 return;                  return;
         }          }
   
         /* Close out the text node, if there is one. */          if (ps->del == 0)
         if (ps->del == 0 && ps->cur != NULL && ps->cur->node == NODE_TEXT) {                  pnode_closetext(ps);
                 pnode_trim(ps->cur);  
                 ps->cur = ps->cur->parent;  
         }  
   
         if (name != NULL) {          if (name != NULL) {
                 for (elem = elements; elem->name != NULL; elem++)                  for (elem = elements; elem->name != NULL; elem++)
Line 558  xml_elem_end(struct parse *ps, const char *name)
Line 564  xml_elem_end(struct parse *ps, const char *name)
                         ps->tree->flags |= TREE_CLOSED;                          ps->tree->flags |= TREE_CLOSED;
                 else                  else
                         ps->cur = ps->cur->parent;                          ps->cur = ps->cur->parent;
                   ps->spc = 0;
                 break;                  break;
         }          }
         assert(ps->del == 0);          assert(ps->del == 0);
Line 651  parse_string(struct parse *p, char *b, size_t rlen,
Line 658  parse_string(struct parse *p, char *b, size_t rlen,
         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 = 0;          pend = 0;
         for (;;) {          for (;;) {
   
Line 663  parse_string(struct parse *p, char *b, size_t rlen,
Line 671  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;
                         increment(p, b, &pend, refill);                          increment(p, b, &pend, refill);
                         continue;                          continue;
                 }                  }
Line 836  parse_file(struct parse *p, int fd, const char *fname)
Line 845  parse_file(struct parse *p, int fd, const char *fname)
                 perror(fname);                  perror(fname);
                 p->tree->flags |= TREE_FAIL;                  p->tree->flags |= TREE_FAIL;
         }          }
         if (p->cur != NULL && p->cur->node == NODE_TEXT) {          pnode_closetext(p);
                 pnode_trim(p->cur);  
                 p->cur = p->cur->parent;  
         }  
         if ((p->tree->flags & TREE_CLOSED) == 0)          if ((p->tree->flags & TREE_CLOSED) == 0)
                 warn_msg(p, "document not closed");                  warn_msg(p, "document not closed");
         return p->tree;          return p->tree;

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.16

CVSweb