=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.30 retrieving revision 1.34 diff -u -p -r1.30 -r1.34 --- docbook2mdoc/parse.c 2019/04/10 14:22:37 1.30 +++ docbook2mdoc/parse.c 2019/04/12 04:39:24 1.34 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.30 2019/04/10 14:22:37 schwarze Exp $ */ +/* $Id: parse.c,v 1.34 2019/04/12 04:39:24 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -325,43 +325,40 @@ static void xml_char(struct parse *p, const char *word, int sz) { struct pnode *n; - size_t newsz; + size_t oldsz, newsz; + assert(sz > 0); if (p->del > 0) return; - if (p->cur == NULL) { + if ((n = p->cur) == NULL) { error_msg(p, "discarding text before document: %.*s", sz, word); return; } - if (p->cur->node != NODE_TEXT) { - if ((n = calloc(1, sizeof(*n))) == NULL) + if (n->node != NODE_TEXT) { + if ((n = pnode_alloc(p->cur)) == NULL) fatal(p); n->node = NODE_TEXT; 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; } - if (p->tree->flags & TREE_CLOSED && - p->cur->parent == p->tree->root) + if (p->tree->flags & TREE_CLOSED && n->parent == p->tree->root) warn_msg(p, "text after end of document: %.*s", sz, word); /* Append to the current text node. */ - assert(sz >= 0); - newsz = p->cur->bsz + (p->cur->bsz && (p->flags & PFLAG_SPC)) + sz; - if ((p->cur->b = realloc(p->cur->b, newsz + 1)) == NULL) + oldsz = n->b == NULL ? 0 : strlen(n->b); + newsz = oldsz + sz; + if (oldsz && (p->flags & PFLAG_SPC)) + newsz++; + if ((n->b = realloc(n->b, newsz + 1)) == NULL) fatal(p); - if (p->cur->bsz && (p->flags & PFLAG_SPC)) - p->cur->b[p->cur->bsz++] = ' '; - memcpy(p->cur->b + p->cur->bsz, word, sz); - p->cur->b[p->cur->bsz = newsz] = '\0'; - p->cur->real = p->cur->b; + if (oldsz && (p->flags & PFLAG_SPC)) + n->b[oldsz++] = ' '; + memcpy(n->b + oldsz, word, sz); + n->b[newsz] = '\0'; p->flags &= ~PFLAG_SPC; } @@ -372,14 +369,15 @@ static void pnode_closetext(struct parse *p) { struct pnode *n; + char *cp; if ((n = p->cur) == NULL || n->node != NODE_TEXT) return; p->cur = n->parent; - while (n->bsz > 0 && isspace((unsigned char)n->b[n->bsz - 1])) { - n->b[--n->bsz] = '\0'; + for (cp = strchr(n->b, '\0'); + cp > n->b && isspace((unsigned char)cp[-1]); + *--cp = '\0') p->flags |= PFLAG_SPC; - } } static void @@ -438,16 +436,11 @@ xml_entity(struct parse *p, const char *name) } /* Create, append, and close out an entity node. */ - if ((n = calloc(1, sizeof(*n))) == NULL || - (n->b = n->real = strdup(entity->roff)) == NULL) + if ((n = pnode_alloc(p->cur)) == NULL || + (n->b = strdup(entity->roff)) == NULL) fatal(p); n->node = NODE_ESCAPE; - n->bsz = strlen(n->b); 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; } @@ -503,7 +496,7 @@ xml_elem_start(struct parse *p, const char *name) if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL) 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); /* @@ -555,13 +548,6 @@ xml_elem_start(struct parse *p, const char *name) n->spc = (p->flags & PFLAG_SPC) != 0; 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; if (n->node == NODE_DOCTYPE) { if (p->doctype == NULL) @@ -632,7 +618,7 @@ xml_attrval(struct parse *p, const char *name) * If we're at a text node, roll that one up first. */ 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; @@ -643,13 +629,13 @@ xml_elem_end(struct parse *ps, const char *name) * An ancestor is excluded from the tree; * keep track of the number of levels excluded. */ - if (ps->del > 1) { - ps->del--; + if (p->del > 1) { + p->del--; return; } - if (ps->del == 0) - pnode_closetext(ps); + if (p->del == 0) + pnode_closetext(p); if (name != NULL) { for (elem = elements; elem->name != NULL; elem++) @@ -657,34 +643,35 @@ xml_elem_end(struct parse *ps, const char *name) break; node = elem->node; } else - node = ps->ncur; + node = p->ncur; switch (node) { case NODE_DELETE_WARN: case NODE_DELETE: - if (ps->del > 0) - ps->del--; + if (p->del > 0) + p->del--; break; case NODE_IGNORE: break; case NODE_INCLUDE: - n = ps->cur; - ps->cur = ps->cur->parent; + n = p->cur; + p->cur = p->cur->parent; cp = pnode_getattr_raw(n, ATTRKEY_HREF, NULL); if (cp == NULL) - error_msg(ps, " element " + error_msg(p, " element " "without href attribute"); else - parse_file(ps, -1, cp); + parse_file(p, -1, cp); pnode_unlink(n); - ps->flags &= ~PFLAG_SPC; + p->flags &= ~PFLAG_SPC; break; case NODE_DOCTYPE: - ps->flags &= ~PFLAG_EEND; + case NODE_SBR: + p->flags &= ~PFLAG_EEND; /* FALLTHROUGH */ default: - if (ps->cur == NULL || node != ps->cur->node) { - warn_msg(ps, "element not open: ", name); + if (p->cur == NULL || node != p->cur->node) { + warn_msg(p, "element not open: ", name); break; } @@ -695,16 +682,16 @@ xml_elem_end(struct parse *ps, const char *name) * obviously better than discarding it or crashing. */ - if (ps->cur->parent != NULL || node == NODE_DOCTYPE) { - ps->cur = ps->cur->parent; - if (ps->cur != NULL) - ps->ncur = ps->cur->node; + if (p->cur->parent != NULL || node == NODE_DOCTYPE) { + p->cur = p->cur->parent; + if (p->cur != NULL) + p->ncur = p->cur->node; } else - ps->tree->flags |= TREE_CLOSED; - ps->flags &= ~PFLAG_SPC; + p->tree->flags |= TREE_CLOSED; + p->flags &= ~PFLAG_SPC; break; } - assert(ps->del == 0); + assert(p->del == 0); } struct parse * @@ -970,9 +957,11 @@ parse_string(struct parse *p, char *b, size_t rlen, } else { advance(p, b, rlen, &pend, - p->ncur == NODE_DOCTYPE ? "<&]" : "<&", + p->ncur == NODE_DOCTYPE ? "<&]\n" : "<&\n", refill); xml_char(p, b + poff, pend - poff); + if (b[pend] == '\n') + pnode_closetext(p); } } return poff;