=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.31 retrieving revision 1.32 diff -u -p -r1.31 -r1.32 --- docbook2mdoc/parse.c 2019/04/10 14:34:08 1.31 +++ docbook2mdoc/parse.c 2019/04/11 04:23:22 1.32 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.31 2019/04/10 14:34:08 schwarze Exp $ */ +/* $Id: parse.c,v 1.32 2019/04/11 04:23:22 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -325,17 +325,18 @@ 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->node != NODE_TEXT) { if ((n = calloc(1, sizeof(*n))) == NULL) fatal(p); n->node = NODE_TEXT; @@ -347,21 +348,21 @@ xml_char(struct parse *p, const char *word, int sz) 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 +373,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 @@ -439,10 +441,9 @@ 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) + (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); @@ -680,6 +681,7 @@ xml_elem_end(struct parse *p, const char *name) p->flags &= ~PFLAG_SPC; break; case NODE_DOCTYPE: + case NODE_SBR: p->flags &= ~PFLAG_EEND; /* FALLTHROUGH */ default: