=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.51 retrieving revision 1.55 diff -u -p -r1.51 -r1.55 --- docbook2mdoc/parse.c 2019/04/24 18:38:02 1.51 +++ docbook2mdoc/parse.c 2019/04/29 02:00:50 1.55 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.51 2019/04/24 18:38:02 schwarze Exp $ */ +/* $Id: parse.c,v 1.55 2019/04/29 02:00:50 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -15,6 +15,8 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include + #include #include #include @@ -26,6 +28,7 @@ #include #include +#include "xmalloc.h" #include "node.h" #include "parse.h" @@ -96,6 +99,7 @@ static const struct alias aliases[] = { { "informaltable", NODE_TABLE }, { "keycap", NODE_KEYSYM }, { "keycode", NODE_IGNORE }, + { "keycombo", NODE_IGNORE }, { "mediaobject", NODE_BLOCKQUOTE }, { "orgname", NODE_IGNORE }, { "othercredit", NODE_AUTHOR }, @@ -104,6 +108,7 @@ static const struct alias aliases[] = { { "phrase", NODE_IGNORE }, { "primary", NODE_DELETE }, { "property", NODE_PARAMETER }, + { "reference", NODE_SECTION }, { "refsect1", NODE_SECTION }, { "refsect2", NODE_SECTION }, { "refsect3", NODE_SECTION }, @@ -191,14 +196,6 @@ 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; @@ -256,8 +253,7 @@ xml_text(struct parse *p, const char *word, int sz) newsz = oldsz + sz; if (oldsz && (p->flags & PFLAG_SPC)) newsz++; - if ((n->b = realloc(n->b, newsz + 1)) == NULL) - fatal(p); + n->b = xrealloc(n->b, newsz + 1); if (oldsz && (p->flags & PFLAG_SPC)) n->b[oldsz++] = ' '; memcpy(n->b + oldsz, word, sz); @@ -271,8 +267,7 @@ xml_text(struct parse *p, const char *word, int sz) /* Create a new text node. */ - if ((n = pnode_alloc(p->cur)) == NULL) - fatal(p); + n = pnode_alloc(p->cur); n->node = NODE_TEXT; n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) | ((p->flags & PFLAG_SPC) ? NFLAG_SPC : 0); @@ -306,8 +301,7 @@ xml_text(struct parse *p, const char *word, int sz) i = 0; while (i < sz && !isspace((unsigned char)word[i])) i++; - if ((n->b = strndup(word, i)) == NULL) - fatal(p); + n->b = xstrndup(word, i); if (i == sz) return; while (i < sz && isspace((unsigned char)word[i])) @@ -319,15 +313,13 @@ xml_text(struct parse *p, const char *word, int sz) /* Put any remaining text into a second node. */ - if ((n = pnode_alloc(p->cur)) == NULL) - fatal(p); + n = pnode_alloc(p->cur); n->node = NODE_TEXT; n->flags |= NFLAG_SPC; word += i; sz -= i; } - if ((n->b = strndup(word, sz)) == NULL) - fatal(p); + n->b = xstrndup(word, sz); /* The new node remains open for later pnode_closetext(). */ @@ -370,12 +362,8 @@ pnode_closetext(struct parse *p, int check_last_word) /* 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 = pnode_alloc_text(p->cur, last_word); n->flags |= NFLAG_SPC; - if ((n->b = strdup(last_word)) == NULL) - fatal(p); } static void @@ -421,8 +409,7 @@ xml_entity(struct parse *p, const char *name) if ((ccp = pnode_getattr_raw(n, ATTRKEY_DEFINITION, NULL)) == NULL) continue; - if ((cp = strdup(ccp)) == NULL) - fatal(p); + cp = xstrdup(ccp); pstate = PARSE_ELEM; parse_string(p, cp, strlen(cp), &pstate, 0); p->flags &= ~(PFLAG_LINE | PFLAG_SPC); @@ -433,10 +420,8 @@ xml_entity(struct parse *p, const char *name) if (*name == '#') { codepoint = strtonum(name + 1, 0, 0x10ffff, &ccp); if (ccp == NULL) { - if ((n = pnode_alloc(p->cur)) == NULL || - asprintf(&n->b, "\\[u%4.4X]", - codepoint) < 0) - fatal(p); + n = pnode_alloc(p->cur); + xasprintf(&n->b, "\\[u%4.4X]", codepoint); goto done; } } @@ -445,9 +430,8 @@ xml_entity(struct parse *p, const char *name) } /* Create, append, and close out an entity node. */ - if ((n = pnode_alloc(p->cur)) == NULL || - (n->b = strdup(entity->roff)) == NULL) - fatal(p); + n = pnode_alloc(p->cur); + n->b = xstrdup(entity->roff); done: n->node = NODE_ESCAPE; n->flags = ((p->flags & PFLAG_LINE) ? NFLAG_LINE : 0) | @@ -522,8 +506,7 @@ xml_elem_start(struct parse *p, const char *name) break; } - if ((n = pnode_alloc(p->cur)) == NULL) - fatal(p); + n = pnode_alloc(p->cur); /* * Some elements are self-closing. @@ -585,17 +568,14 @@ xml_attrkey(struct parse *p, const char *name) p->flags &= ~PFLAG_ATTR; return; } - if ((a = calloc(1, sizeof(*a))) == NULL) - fatal(p); - + a = xcalloc(1, sizeof(*a)); a->key = key; a->val = ATTRVAL__MAX; if (value == NULL) { a->rawval = NULL; p->flags |= PFLAG_ATTR; } else { - if ((a->rawval = strdup(value)) == NULL) - fatal(p); + a->rawval = xstrdup(value); p->flags &= ~PFLAG_ATTR; } TAILQ_INSERT_TAIL(&p->cur->attrq, a, child); @@ -613,9 +593,8 @@ xml_attrval(struct parse *p, const char *name) return; if ((a = TAILQ_LAST(&p->cur->attrq, pattrq)) == NULL) return; - if ((a->val = attrval_parse(name)) == ATTRVAL__MAX && - (a->rawval = strdup(name)) == NULL) - fatal(p); + if ((a->val = attrval_parse(name)) == ATTRVAL__MAX) + a->rawval = xstrdup(name); p->flags &= ~PFLAG_ATTR; } @@ -710,13 +689,8 @@ parse_alloc(int warn) { struct parse *p; - if ((p = calloc(1, sizeof(*p))) == NULL) - return NULL; - - if ((p->tree = calloc(1, sizeof(*p->tree))) == NULL) { - free(p); - return NULL; - } + p = xcalloc(1, sizeof(*p)); + p->tree = xcalloc(1, sizeof(*p->tree)); if (warn) p->flags |= PFLAG_WARN; else