diff options
-rw-r--r-- | libman.h | 1 | ||||
-rw-r--r-- | libmandoc.h | 1 | ||||
-rw-r--r-- | libmdoc.h | 1 | ||||
-rw-r--r-- | main.c | 18 | ||||
-rw-r--r-- | man.c | 51 | ||||
-rw-r--r-- | man.h | 2 | ||||
-rw-r--r-- | man_action.c | 11 | ||||
-rw-r--r-- | mandoc.c | 13 | ||||
-rw-r--r-- | mdoc.c | 58 | ||||
-rw-r--r-- | mdoc.h | 2 | ||||
-rw-r--r-- | mdoc_argv.c | 2 |
11 files changed, 38 insertions, 122 deletions
@@ -41,7 +41,6 @@ struct man { enum merr { WNPRINT = 0, - WNMEM, WMSEC, WDATE, WLNSCOPE, diff --git a/libmandoc.h b/libmandoc.h index b147f465..5f9ad688 100644 --- a/libmandoc.h +++ b/libmandoc.h @@ -24,7 +24,6 @@ void *mandoc_calloc(size_t, size_t); char *mandoc_strdup(const char *); void *mandoc_malloc(size_t); void *mandoc_realloc(void *, size_t); -void *mandoc_reallocf(void *, size_t); __END_DECLS @@ -44,7 +44,6 @@ enum merr { ETAILWS = 0, EQUOTPARM, EQUOTTERM, - EMALLOC, EARGVAL, EBODYPROL, EPROLBODY, @@ -184,11 +184,9 @@ main(int argc, char *argv[]) argv++; if (*argv && rc) { if (curp.lastman) - if ( ! man_reset(curp.lastman)) - rc = 0; + man_reset(curp.lastman); if (curp.lastmdoc) - if ( ! mdoc_reset(curp.lastmdoc)) - rc = 0; + mdoc_reset(curp.lastmdoc); curp.lastman = NULL; curp.lastmdoc = NULL; } @@ -233,7 +231,6 @@ static struct man * man_init(struct curparse *curp) { int pflags; - struct man *man; struct man_cb mancb; mancb.man_err = merr; @@ -250,10 +247,7 @@ man_init(struct curparse *curp) if (curp->fflags & NO_IGN_ESCAPE) pflags &= ~MAN_IGN_ESCAPE; - if (NULL == (man = man_alloc(curp, pflags, &mancb))) - warnx("memory exhausted"); - - return(man); + return(man_alloc(curp, pflags, &mancb)); } @@ -261,7 +255,6 @@ static struct mdoc * mdoc_init(struct curparse *curp) { int pflags; - struct mdoc *mdoc; struct mdoc_cb mdoccb; mdoccb.mdoc_err = merr; @@ -280,10 +273,7 @@ mdoc_init(struct curparse *curp) if (curp->fflags & NO_IGN_CHARS) pflags &= ~MDOC_IGN_CHARS; - if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb))) - warnx("memory exhausted"); - - return(mdoc); + return(mdoc_alloc(curp, pflags, &mdoccb)); } @@ -24,10 +24,10 @@ #include <string.h> #include "libman.h" +#include "libmandoc.h" const char *const __man_merrnames[WERRMAX] = { "invalid character", /* WNPRINT */ - "system: malloc error", /* WNMEM */ "invalid manual section", /* WMSEC */ "invalid date format", /* WDATE */ "scope of prior line violated", /* WLNSCOPE */ @@ -67,7 +67,7 @@ static int man_node_append(struct man *, static int man_ptext(struct man *, int, char *); static int man_pmacro(struct man *, int, char *); static void man_free1(struct man *); -static int man_alloc1(struct man *); +static void man_alloc1(struct man *); static int pstring(struct man *, int, int, const char *, size_t); static int macrowarn(struct man *, int, const char *); @@ -93,12 +93,12 @@ man_meta(const struct man *m) } -int +void man_reset(struct man *man) { man_free1(man); - return(man_alloc1(man)); + man_alloc1(man); } @@ -116,19 +116,16 @@ man_alloc(void *data, int pflags, const struct man_cb *cb) { struct man *p; - if (NULL == (p = calloc(1, sizeof(struct man)))) - return(NULL); + p = mandoc_calloc(1, sizeof(struct man)); - if ( ! man_alloc1(p)) { - free(p); - return(NULL); - } + if (cb) + memcpy(&p->cb, cb, sizeof(struct man_cb)); man_hash_init(); - p->data = data; p->pflags = pflags; - (void)memcpy(&p->cb, cb, sizeof(struct man_cb)); + + man_alloc1(p); return(p); } @@ -171,19 +168,16 @@ man_free1(struct man *man) } -static int +static void man_alloc1(struct man *m) { memset(&m->meta, 0, sizeof(struct man_meta)); m->flags = 0; - m->last = calloc(1, sizeof(struct man_node)); - if (NULL == m->last) - return(0); + m->last = mandoc_calloc(1, sizeof(struct man_node)); m->first = m->last; m->last->type = MAN_ROOT; m->next = MAN_NEXT_CHILD; - return(1); } @@ -250,10 +244,7 @@ man_node_alloc(int line, int pos, enum man_type type, int tok) { struct man_node *p; - p = calloc(1, sizeof(struct man_node)); - if (NULL == p) - return(NULL); - + p = mandoc_calloc(1, sizeof(struct man_node)); p->line = line; p->pos = pos; p->type = type; @@ -268,8 +259,6 @@ man_elem_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_ELEM, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -283,8 +272,6 @@ man_head_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_HEAD, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -298,8 +285,6 @@ man_body_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_BODY, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -313,8 +298,6 @@ man_block_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_BLOCK, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -330,15 +313,7 @@ pstring(struct man *m, int line, int pos, size_t sv; n = man_node_alloc(line, pos, MAN_TEXT, -1); - if (NULL == n) - return(0); - - n->string = malloc(len + 1); - if (NULL == n->string) { - free(n); - return(0); - } - + n->string = mandoc_malloc(len + 1); sv = strlcpy(n->string, p, len + 1); /* Prohibit truncation. */ @@ -105,7 +105,7 @@ struct man; void man_free(struct man *); struct man *man_alloc(void *, int, const struct man_cb *); -int man_reset(struct man *); +void man_reset(struct man *); int man_parseln(struct man *, int, char *buf); int man_endparse(struct man *); diff --git a/man_action.c b/man_action.c index e0e4139e..876beabf 100644 --- a/man_action.c +++ b/man_action.c @@ -22,6 +22,7 @@ #include <string.h> #include "libman.h" +#include "libmandoc.h" struct actions { int (*post)(struct man *); @@ -141,9 +142,7 @@ post_TH(struct man *m) n = m->last->child; assert(n); - - if (NULL == (m->meta.title = strdup(n->string))) - return(man_nerr(m, n, WNMEM)); + m->meta.title = mandoc_strdup(n->string); /* TITLE ->MSEC<- DATE SOURCE VOL */ @@ -170,14 +169,12 @@ post_TH(struct man *m) /* TITLE MSEC DATE ->SOURCE<- VOL */ if (n && (n = n->next)) - if (NULL == (m->meta.source = strdup(n->string))) - return(man_nerr(m, n, WNMEM)); + m->meta.source = mandoc_strdup(n->string); /* TITLE MSEC DATE SOURCE ->VOL<- */ if (n && (n = n->next)) - if (NULL == (m->meta.vol = strdup(n->string))) - return(man_nerr(m, n, WNMEM)); + m->meta.vol = mandoc_strdup(n->string); /* * The end document shouldn't have the prologue macros as part @@ -150,19 +150,6 @@ mandoc_realloc(void *ptr, size_t size) } -void * -mandoc_reallocf(void *old_ptr, size_t size) /* FIXME: remove (not used) */ -{ - void *ptr; - - ptr = realloc(old_ptr, size); - if (NULL == ptr) - free(old_ptr); - - return(ptr); -} - - char * mandoc_strdup(const char *ptr) { @@ -24,12 +24,12 @@ #include <string.h> #include "libmdoc.h" +#include "libmandoc.h" const char *const __mdoc_merrnames[MERRMAX] = { "trailing whitespace", /* ETAILWS */ "unexpected quoted parameter", /* EQUOTPARM */ "unterminated quoted parameter", /* EQUOTTERM */ - "system: malloc error", /* EMALLOC */ "argument parameter suggested", /* EARGVAL */ "macro disallowed in prologue", /* EBODYPROL */ "macro disallowed in body", /* EPROLBODY */ @@ -137,7 +137,7 @@ const char * const *mdoc_macronames = __mdoc_macronames; const char * const *mdoc_argnames = __mdoc_argnames; static void mdoc_free1(struct mdoc *); -static int mdoc_alloc1(struct mdoc *); +static void mdoc_alloc1(struct mdoc *); static struct mdoc_node *node_alloc(struct mdoc *, int, int, int, enum mdoc_type); static int node_append(struct mdoc *, @@ -194,21 +194,17 @@ mdoc_free1(struct mdoc *mdoc) /* * Allocate all volatile resources (parse tree, meta-data, fields). */ -static int +static void mdoc_alloc1(struct mdoc *mdoc) { memset(&mdoc->meta, 0, sizeof(struct mdoc_meta)); mdoc->flags = 0; mdoc->lastnamed = mdoc->lastsec = SEC_NONE; - mdoc->last = calloc(1, sizeof(struct mdoc_node)); - if (NULL == mdoc->last) - return(0); - + mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node)); mdoc->first = mdoc->last; mdoc->last->type = MDOC_ROOT; mdoc->next = MDOC_NEXT_CHILD; - return(1); } @@ -218,12 +214,12 @@ mdoc_alloc1(struct mdoc *mdoc) * and the parser is ready for re-invocation on a new tree; however, * cross-parse non-volatile data is kept intact. */ -int +void mdoc_reset(struct mdoc *mdoc) { mdoc_free1(mdoc); - return(mdoc_alloc1(mdoc)); + mdoc_alloc1(mdoc); } @@ -248,21 +244,17 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb) { struct mdoc *p; - if (NULL == (p = calloc(1, sizeof(struct mdoc)))) - return(NULL); - if (cb) - (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); + p = mandoc_calloc(1, sizeof(struct mdoc)); - mdoc_hash_init(); + if (cb) + memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); p->data = data; p->pflags = pflags; - if (mdoc_alloc1(p)) - return(p); - - free(p); - return(NULL); + mdoc_hash_init(); + mdoc_alloc1(p); + return(p); } @@ -438,11 +430,7 @@ node_alloc(struct mdoc *m, int line, { struct mdoc_node *p; - if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(NULL); - } - + p = mandoc_calloc(1, sizeof(struct mdoc_node)); p->sec = m->lastsec; p->line = line; p->pos = pos; @@ -460,8 +448,6 @@ mdoc_tail_alloc(struct mdoc *m, int line, int pos, int tok) struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_TAIL); - if (NULL == p) - return(0); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; @@ -478,8 +464,6 @@ mdoc_head_alloc(struct mdoc *m, int line, int pos, int tok) assert(m->last); p = node_alloc(m, line, pos, tok, MDOC_HEAD); - if (NULL == p) - return(0); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; @@ -493,8 +477,6 @@ mdoc_body_alloc(struct mdoc *m, int line, int pos, int tok) struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BODY); - if (NULL == p) - return(0); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; @@ -509,8 +491,6 @@ mdoc_block_alloc(struct mdoc *m, int line, int pos, struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BLOCK); - if (NULL == p) - return(0); p->args = args; if (p->args) (args->refcnt)++; @@ -528,8 +508,6 @@ mdoc_elem_alloc(struct mdoc *m, int line, int pos, struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_ELEM); - if (NULL == p) - return(0); p->args = args; if (p->args) (args->refcnt)++; @@ -547,15 +525,7 @@ pstring(struct mdoc *m, int line, int pos, const char *p, size_t len) size_t sv; n = node_alloc(m, line, pos, -1, MDOC_TEXT); - if (NULL == n) - return(mdoc_nerr(m, m->last, EMALLOC)); - - n->string = malloc(len + 1); - if (NULL == n->string) { - free(n); - return(mdoc_nerr(m, m->last, EMALLOC)); - } - + n->string = mandoc_malloc(len + 1); sv = strlcpy(n->string, p, len + 1); /* Prohibit truncation. */ @@ -292,7 +292,7 @@ struct mdoc; void mdoc_free(struct mdoc *); struct mdoc *mdoc_alloc(void *, int, const struct mdoc_cb *); -int mdoc_reset(struct mdoc *); +void mdoc_reset(struct mdoc *); int mdoc_parseln(struct mdoc *, int, char *buf); const struct mdoc_node *mdoc_node(const struct mdoc *); const struct mdoc_meta *mdoc_meta(const struct mdoc *); diff --git a/mdoc_argv.c b/mdoc_argv.c index d7f3ca34..726a3f3f 100644 --- a/mdoc_argv.c +++ b/mdoc_argv.c @@ -268,7 +268,7 @@ mdoc_argv(struct mdoc *m, int line, int tok, return(ARGV_ERROR); if (NULL == (arg = *v)) - arg = mandoc_calloc(1, sizeof(struct mdoc_arg)); + arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg)); arg->argc++; arg->argv = mandoc_realloc |