=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.28 retrieving revision 1.37 diff -u -p -r1.28 -r1.37 --- docbook2mdoc/parse.c 2019/04/09 13:35:29 1.28 +++ docbook2mdoc/parse.c 2019/04/12 07:53:09 1.37 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.28 2019/04/09 13:35:29 schwarze Exp $ */ +/* $Id: parse.c,v 1.37 2019/04/12 07:53:09 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -89,6 +89,7 @@ static const struct element elements[] = { { "code", NODE_LITERAL }, { "colspec", NODE_COLSPEC }, { "command", NODE_COMMAND }, + { "computeroutput", NODE_LITERAL }, { "constant", NODE_CONSTANT }, { "contrib", NODE_CONTRIB }, { "copyright", NODE_COPYRIGHT }, @@ -134,6 +135,7 @@ static const struct element elements[] = { { "literal", NODE_LITERAL }, { "literallayout", NODE_LITERALLAYOUT }, { "manvolnum", NODE_MANVOLNUM }, + { "markup", NODE_MARKUP }, { "member", NODE_MEMBER }, { "mml:math", NODE_MML_MATH }, { "mml:mfenced", NODE_MML_MFENCED }, @@ -186,7 +188,7 @@ static const struct element elements[] = { { "sect1", NODE_SECTION }, { "sect2", NODE_SECTION }, { "section", NODE_SECTION }, - { "sgmltag", NODE_SGMLTAG }, + { "sgmltag", NODE_MARKUP }, { "simpara", NODE_PARA }, { "simplelist", NODE_SIMPLELIST }, { "spanspec", NODE_SPANSPEC }, @@ -280,16 +282,24 @@ static void parse_fd(struct parse *, int); static void +fatal(struct parse *p) +{ + fprintf(stderr, "%s:%d:%d: FATAL: ", p->fname, p->line, p->col); + perror(NULL); + exit(6); +} + +static void error_msg(struct parse *p, const char *fmt, ...) { va_list ap; - fprintf(stderr, "%s:%d:%d: ", p->fname, p->line, p->col); + fprintf(stderr, "%s:%d:%d: ERROR: ", p->fname, p->line, p->col); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fputc('\n', stderr); - p->tree->flags |= TREE_FAIL; + p->tree->flags |= TREE_ERROR; } static void @@ -300,11 +310,12 @@ warn_msg(struct parse *p, const char *fmt, ...) if ((p->flags & PFLAG_WARN) == 0) return; - fprintf(stderr, "%s:%d:%d: warning: ", p->fname, p->line, p->col); + fprintf(stderr, "%s:%d:%d: WARNING: ", p->fname, p->line, p->col); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fputc('\n', stderr); + p->tree->flags |= TREE_WARN; } /* @@ -313,76 +324,139 @@ warn_msg(struct parse *p, const char *fmt, ...) * Otherwise, create a new one as a child of the current node. */ static void -xml_char(struct parse *ps, const char *p, int sz) +xml_text(struct parse *p, const char *word, int sz) { - struct pnode *dat; - size_t newsz; + struct pnode *n, *np; + size_t oldsz, newsz; + int i; - if (ps->del > 0) + assert(sz > 0); + if (p->del > 0) return; - if (ps->cur == NULL) { - error_msg(ps, "discarding text before document: %.*s", sz, p); + if ((n = p->cur) == NULL) { + error_msg(p, "discarding text before document: %.*s", + sz, word); return; } - if (ps->cur->node != NODE_TEXT) { - if ((dat = calloc(1, sizeof(*dat))) == NULL) { - perror(NULL); - exit(1); - } - dat->node = NODE_TEXT; - dat->spc = (ps->flags & PFLAG_SPC) != 0; - dat->parent = ps->cur; - TAILQ_INIT(&dat->childq); - TAILQ_INIT(&dat->attrq); - TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); - ps->cur = dat; + /* Append to the current text node, if one is open. */ + + if (n->node == NODE_TEXT) { + oldsz = 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 (oldsz && (p->flags & PFLAG_SPC)) + n->b[oldsz++] = ' '; + memcpy(n->b + oldsz, word, sz); + n->b[newsz] = '\0'; + p->flags &= ~PFLAG_SPC; + return; } - if (ps->tree->flags & TREE_CLOSED && - ps->cur->parent == ps->tree->root) - warn_msg(ps, "text after end of document: %.*s", sz, p); + if (p->tree->flags & TREE_CLOSED && n == p->tree->root) + warn_msg(p, "text after end of document: %.*s", sz, word); - /* Append to the current text node. */ + /* Create a new text node. */ - assert(sz >= 0); - newsz = ps->cur->bsz + (ps->cur->bsz && (ps->flags & PFLAG_SPC)) + sz; - ps->cur->b = realloc(ps->cur->b, newsz + 1); - if (ps->cur->b == NULL) { - perror(NULL); - exit(1); + if ((n = pnode_alloc(p->cur)) == NULL) + fatal(p); + n->node = NODE_TEXT; + n->spc = (p->flags & PFLAG_SPC) != 0; + p->flags &= ~PFLAG_SPC; + + /* + * If this node follows a non-text node without intervening + * whitespace, keep the text in it as short as possible, + * and do not keep it open. + */ + + if (n->spc == 0 && + (np = TAILQ_PREV(n, pnodeq, child)) != NULL && + np->node != NODE_TEXT && np->node != NODE_ESCAPE) { + i = 0; + while (i < sz && !isspace((unsigned char)word[i])) + i++; + if ((n->b = strndup(word, i)) == NULL) + fatal(p); + if (i == sz) + return; + while (i < sz && isspace((unsigned char)word[i])) + i++; + if (i == sz) { + p->flags |= PFLAG_SPC; + return; + } + + /* Put any remaining text into a second node. */ + + if ((n = pnode_alloc(p->cur)) == NULL) + fatal(p); + n->node = NODE_TEXT; + n->spc = 1; + word += i; + sz -= i; } - if (ps->cur->bsz && (ps->flags & PFLAG_SPC)) - ps->cur->b[ps->cur->bsz++] = ' '; - memcpy(ps->cur->b + ps->cur->bsz, p, sz); - ps->cur->b[ps->cur->bsz = newsz] = '\0'; - ps->cur->real = ps->cur->b; - ps->flags &= ~PFLAG_SPC; + if ((n->b = strndup(word, sz)) == NULL) + fatal(p); + + /* The new node remains open for later pnode_closetext(). */ + + p->cur = n; } /* * Close out the text node and strip trailing whitespace, if one is open. */ static void -pnode_closetext(struct parse *p) +pnode_closetext(struct parse *p, int check_last_word) { struct pnode *n; + char *cp, *last_word; 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; - } + + if (p->flags & PFLAG_SPC || !check_last_word) + return; + + /* + * Find the beginning of the last word + * and delete whitespace before it. + */ + + while (cp > n->b && !isspace((unsigned char)cp[-1])) + cp--; + if (cp == n->b) + return; + + last_word = cp; + while (cp > n->b && isspace((unsigned char)cp[-1])) + *--cp = '\0'; + + /* Move the last word into its own node, for use with .Pf. */ + + if ((n = pnode_alloc(p->cur)) == NULL) + fatal(p); + n->node = NODE_TEXT; + n->spc = 1; + if ((n->b = strdup(last_word)) == NULL) + fatal(p); } static void xml_entity(struct parse *p, const char *name) { const struct entity *entity; - struct pnode *dat; + struct pnode *n; const char *ccp; char *cp; enum pstate pstate; @@ -395,7 +469,7 @@ xml_entity(struct parse *p, const char *name) return; } - pnode_closetext(p); + pnode_closetext(p, 0); if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root) warn_msg(p, "entity after end of document: &%s;", name); @@ -406,24 +480,22 @@ xml_entity(struct parse *p, const char *name) if (entity->roff == NULL) { if (p->doctype != NULL) { - TAILQ_FOREACH(dat, &p->doctype->childq, child) { - if ((ccp = pnode_getattr_raw(dat, + TAILQ_FOREACH(n, &p->doctype->childq, child) { + if ((ccp = pnode_getattr_raw(n, ATTRKEY_NAME, NULL)) == NULL || strcmp(ccp, name) != 0) continue; - if ((ccp = pnode_getattr_raw(dat, + if ((ccp = pnode_getattr_raw(n, ATTRKEY_SYSTEM, NULL)) != NULL) { parse_file(p, -1, ccp); p->flags &= ~PFLAG_SPC; return; } - if ((ccp = pnode_getattr_raw(dat, + if ((ccp = pnode_getattr_raw(n, ATTRKEY_DEFINITION, NULL)) == NULL) continue; - if ((cp = strdup(ccp)) == NULL) { - perror(NULL); - exit(1); - } + if ((cp = strdup(ccp)) == NULL) + fatal(p); pstate = PARSE_ELEM; parse_string(p, cp, strlen(cp), &pstate, 0); p->flags &= ~PFLAG_SPC; @@ -436,18 +508,11 @@ xml_entity(struct parse *p, const char *name) } /* Create, append, and close out an entity node. */ - if ((dat = calloc(1, sizeof(*dat))) == NULL || - (dat->b = dat->real = strdup(entity->roff)) == NULL) { - perror(NULL); - exit(1); - } - dat->node = NODE_ESCAPE; - dat->bsz = strlen(dat->b); - dat->spc = (p->flags & PFLAG_SPC) != 0; - dat->parent = p->cur; - TAILQ_INIT(&dat->childq); - TAILQ_INIT(&dat->attrq); - TAILQ_INSERT_TAIL(&p->cur->childq, dat, child); + if ((n = pnode_alloc(p->cur)) == NULL || + (n->b = strdup(entity->roff)) == NULL) + fatal(p); + n->node = NODE_ESCAPE; + n->spc = (p->flags & PFLAG_SPC) != 0; p->flags &= ~PFLAG_SPC; } @@ -455,22 +520,22 @@ xml_entity(struct parse *p, const char *name) * Begin an element. */ static void -xml_elem_start(struct parse *ps, const char *name) +xml_elem_start(struct parse *p, const char *name) { const struct element *elem; - struct pnode *dat; + struct pnode *n; /* * An ancestor is excluded from the tree; * keep track of the number of levels excluded. */ - if (ps->del > 0) { + if (p->del > 0) { if (*name != '!' && *name != '?') - ps->del++; + p->del++; return; } - pnode_closetext(ps); + pnode_closetext(p, 1); for (elem = elements; elem->name != NULL; elem++) if (strcmp(elem->name, name) == 0) @@ -479,45 +544,43 @@ xml_elem_start(struct parse *ps, const char *name) if (elem->name == NULL) { if (*name == '!' || *name == '?') return; - error_msg(ps, "unknown element <%s>", name); + error_msg(p, "unknown element <%s>", name); } - ps->ncur = elem->node; + p->ncur = elem->node; - switch (ps->ncur) { + switch (p->ncur) { case NODE_DELETE_WARN: - warn_msg(ps, "skipping element <%s>", name); + warn_msg(p, "skipping element <%s>", name); /* FALLTHROUGH */ case NODE_DELETE: - ps->del = 1; + p->del = 1; /* FALLTHROUGH */ case NODE_IGNORE: return; case NODE_INLINEEQUATION: - ps->tree->flags |= TREE_EQN; + p->tree->flags |= TREE_EQN; break; default: break; } - if (ps->tree->flags & TREE_CLOSED && ps->cur->parent == NULL) - warn_msg(ps, "element after end of document: <%s>", name); + if (p->tree->flags & TREE_CLOSED && p->cur->parent == NULL) + warn_msg(p, "element after end of document: <%s>", name); - if ((dat = calloc(1, sizeof(*dat))) == NULL) { - perror(NULL); - exit(1); - } + if ((n = pnode_alloc(p->cur)) == NULL) + fatal(p); /* * Nodes that begin a new macro or request line or start by * printing text always want whitespace before themselves. */ - switch (dat->node = elem->node) { + switch (n->node = elem->node) { case NODE_DOCTYPE: case NODE_ENTITY: case NODE_SBR: - ps->flags |= PFLAG_EEND; + p->flags |= PFLAG_EEND; /* FALLTHROUGH */ case NODE_APPENDIX: case NODE_AUTHORGROUP: @@ -551,87 +614,75 @@ xml_elem_start(struct parse *ps, const char *name) case NODE_VARIABLELIST: case NODE_VARLISTENTRY: case NODE_WARNING: - dat->spc = 1; + n->spc = 1; break; default: - dat->spc = (ps->flags & PFLAG_SPC) != 0; + n->spc = (p->flags & PFLAG_SPC) != 0; break; } - dat->parent = ps->cur; - TAILQ_INIT(&dat->childq); - TAILQ_INIT(&dat->attrq); - - if (ps->cur != NULL) - TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child); - - ps->cur = dat; - if (dat->node == NODE_DOCTYPE) { - if (ps->doctype == NULL) - ps->doctype = dat; + p->cur = n; + if (n->node == NODE_DOCTYPE) { + if (p->doctype == NULL) + p->doctype = n; else - error_msg(ps, "duplicate doctype"); - } else if (dat->parent == NULL && ps->tree->root == NULL) - ps->tree->root = dat; + error_msg(p, "duplicate doctype"); + } else if (n->parent == NULL && p->tree->root == NULL) + p->tree->root = n; } static void -xml_attrkey(struct parse *ps, const char *name) +xml_attrkey(struct parse *p, const char *name) { - struct pattr *attr; + struct pattr *a; const char *value; enum attrkey key; - if (ps->del > 0 || ps->ncur == NODE_IGNORE || *name == '\0') + if (p->del > 0 || p->ncur == NODE_IGNORE || *name == '\0') return; - if ((ps->ncur == NODE_DOCTYPE || ps->ncur == NODE_ENTITY) && - TAILQ_FIRST(&ps->cur->attrq) == NULL) { + if ((p->ncur == NODE_DOCTYPE || p->ncur == NODE_ENTITY) && + TAILQ_FIRST(&p->cur->attrq) == NULL) { value = name; name = "NAME"; } else value = NULL; if ((key = attrkey_parse(name)) == ATTRKEY__MAX) { - ps->flags &= ~PFLAG_ATTR; + p->flags &= ~PFLAG_ATTR; return; } - if ((attr = calloc(1, sizeof(*attr))) == NULL) { - perror(NULL); - exit(1); - } - attr->key = key; - attr->val = ATTRVAL__MAX; + if ((a = calloc(1, sizeof(*a))) == NULL) + fatal(p); + + a->key = key; + a->val = ATTRVAL__MAX; if (value == NULL) { - attr->rawval = NULL; - ps->flags |= PFLAG_ATTR; + a->rawval = NULL; + p->flags |= PFLAG_ATTR; } else { - if ((attr->rawval = strdup(value)) == NULL) { - perror(NULL); - exit(1); - } - ps->flags &= ~PFLAG_ATTR; + if ((a->rawval = strdup(value)) == NULL) + fatal(p); + p->flags &= ~PFLAG_ATTR; } - TAILQ_INSERT_TAIL(&ps->cur->attrq, attr, child); - if (ps->ncur == NODE_ENTITY && key == ATTRKEY_NAME) - xml_attrkey(ps, "DEFINITION"); + TAILQ_INSERT_TAIL(&p->cur->attrq, a, child); + if (p->ncur == NODE_ENTITY && key == ATTRKEY_NAME) + xml_attrkey(p, "DEFINITION"); } static void -xml_attrval(struct parse *ps, const char *name) +xml_attrval(struct parse *p, const char *name) { - struct pattr *attr; + struct pattr *a; - if (ps->del > 0 || ps->ncur == NODE_IGNORE || - (ps->flags & PFLAG_ATTR) == 0) + if (p->del > 0 || p->ncur == NODE_IGNORE || + (p->flags & PFLAG_ATTR) == 0) return; - if ((attr = TAILQ_LAST(&ps->cur->attrq, pattrq)) == NULL) + if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL) return; - if ((attr->val = attrval_parse(name)) == ATTRVAL__MAX && - (attr->rawval = strdup(name)) == NULL) { - perror(NULL); - exit(1); - } - ps->flags &= ~PFLAG_ATTR; + if ((a->val = attrval_parse(name)) == ATTRVAL__MAX && + (a->rawval = strdup(name)) == NULL) + fatal(p); + p->flags &= ~PFLAG_ATTR; } /* @@ -639,7 +690,7 @@ xml_attrval(struct parse *ps, 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; @@ -650,13 +701,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, 0); if (name != NULL) { for (elem = elements; elem->name != NULL; elem++) @@ -664,34 +715,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; } @@ -702,16 +754,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 * @@ -977,9 +1029,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); + xml_text(p, b + poff, pend - poff); + if (b[pend] == '\n') + pnode_closetext(p, 0); } } return poff; @@ -1064,7 +1118,7 @@ parse_file(struct parse *p, int fd, const char *fname) /* On the top level, finalize the parse tree. */ if (save_fname == NULL) { - pnode_closetext(p); + pnode_closetext(p, 0); if (p->tree->root == NULL) error_msg(p, "empty document"); else if ((p->tree->flags & TREE_CLOSED) == 0)