=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.52 retrieving revision 1.53 diff -u -p -r1.52 -r1.53 --- docbook2mdoc/parse.c 2019/04/25 17:57:59 1.52 +++ docbook2mdoc/parse.c 2019/04/28 15:03:29 1.53 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.52 2019/04/25 17:57:59 schwarze Exp $ */ +/* $Id: parse.c,v 1.53 2019/04/28 15:03:29 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" @@ -192,14 +195,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; @@ -257,8 +252,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); @@ -272,8 +266,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); @@ -307,8 +300,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])) @@ -320,15 +312,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(). */ @@ -371,12 +361,10 @@ 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 = pnode_alloc(p->cur); n->node = NODE_TEXT; n->flags |= NFLAG_SPC; - if ((n->b = strdup(last_word)) == NULL) - fatal(p); + n->b = xstrdup(last_word); } static void @@ -422,8 +410,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); @@ -434,10 +421,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; } } @@ -446,9 +431,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) | @@ -523,8 +507,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. @@ -586,17 +569,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); @@ -614,9 +594,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; } @@ -711,13 +690,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