summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2009-10-30 05:58:36 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2009-10-30 05:58:36 +0000
commit8caff3a42d43b9b1377f3802a105817dcd4301dc (patch)
treefa20e17e6b031d6eb44e1bbbf800fa3441b7212a
parent29f8f2c56cb552aed51690e35cbb6697b57f18b4 (diff)
downloadmandoc-8caff3a42d43b9b1377f3802a105817dcd4301dc.tar.gz
mandoc-8caff3a42d43b9b1377f3802a105817dcd4301dc.tar.zst
mandoc-8caff3a42d43b9b1377f3802a105817dcd4301dc.zip
libmdoc and libman now using non-recoverable allocations (simpler code).
-rw-r--r--libman.h3
-rw-r--r--libmandoc.h3
-rw-r--r--libmdoc.h3
-rw-r--r--main.c20
-rw-r--r--man.c53
-rw-r--r--man.h4
-rw-r--r--man_action.c13
-rw-r--r--mandoc.c15
-rw-r--r--mdoc.c60
-rw-r--r--mdoc.h4
-rw-r--r--mdoc_argv.c4
11 files changed, 49 insertions, 133 deletions
diff --git a/libman.h b/libman.h
index 3b7c70e9..c0b5cb46 100644
--- a/libman.h
+++ b/libman.h
@@ -1,4 +1,4 @@
-/* $Id: libman.h,v 1.22 2009/10/26 07:11:06 kristaps Exp $ */
+/* $Id: libman.h,v 1.23 2009/10/30 05:58:36 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -41,7 +41,6 @@ struct man {
enum merr {
WNPRINT = 0,
- WNMEM,
WMSEC,
WDATE,
WLNSCOPE,
diff --git a/libmandoc.h b/libmandoc.h
index 017e4ed6..696c923f 100644
--- a/libmandoc.h
+++ b/libmandoc.h
@@ -1,4 +1,4 @@
-/* $Id: libmandoc.h,v 1.2 2009/10/28 19:21:59 kristaps Exp $ */
+/* $Id: libmandoc.h,v 1.3 2009/10/30 05:58:37 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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
diff --git a/libmdoc.h b/libmdoc.h
index 80ab5275..9c5dace5 100644
--- a/libmdoc.h
+++ b/libmdoc.h
@@ -1,4 +1,4 @@
-/* $Id: libmdoc.h,v 1.29 2009/10/15 02:56:51 kristaps Exp $ */
+/* $Id: libmdoc.h,v 1.30 2009/10/30 05:58:37 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -44,7 +44,6 @@ enum merr {
ETAILWS = 0,
EQUOTPARM,
EQUOTTERM,
- EMALLOC,
EARGVAL,
EBODYPROL,
EPROLBODY,
diff --git a/main.c b/main.c
index 43e8f511..943bd620 100644
--- a/main.c
+++ b/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.51 2009/10/27 08:26:11 kristaps Exp $ */
+/* $Id: main.c,v 1.52 2009/10/30 05:58:37 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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));
}
diff --git a/man.c b/man.c
index 3631ef25..1cc0811a 100644
--- a/man.c
+++ b/man.c
@@ -1,4 +1,4 @@
-/* $Id: man.c,v 1.44 2009/10/27 08:26:12 kristaps Exp $ */
+/* $Id: man.c,v 1.45 2009/10/30 05:58:37 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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. */
diff --git a/man.h b/man.h
index 0c59d5bc..efb8568c 100644
--- a/man.h
+++ b/man.h
@@ -1,4 +1,4 @@
-/* $Id: man.h,v 1.22 2009/10/24 05:45:04 kristaps Exp $ */
+/* $Id: man.h,v 1.23 2009/10/30 05:58:37 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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 a43c72ef..838cd263 100644
--- a/man_action.c
+++ b/man_action.c
@@ -1,4 +1,4 @@
-/* $Id: man_action.c,v 1.21 2009/10/27 08:26:12 kristaps Exp $ */
+/* $Id: man_action.c,v 1.22 2009/10/30 05:58:37 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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
diff --git a/mandoc.c b/mandoc.c
index 91a0fa92..068b365d 100644
--- a/mandoc.c
+++ b/mandoc.c
@@ -1,4 +1,4 @@
-/* $Id: mandoc.c,v 1.4 2009/10/28 19:21:59 kristaps Exp $ */
+/* $Id: mandoc.c,v 1.5 2009/10/30 05:58:38 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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)
{
diff --git a/mdoc.c b/mdoc.c
index 0118c7a8..558782e3 100644
--- a/mdoc.c
+++ b/mdoc.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc.c,v 1.112 2009/10/27 08:26:12 kristaps Exp $ */
+/* $Id: mdoc.c,v 1.113 2009/10/30 05:58:38 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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. */
diff --git a/mdoc.h b/mdoc.h
index 810a4d0f..184d08e1 100644
--- a/mdoc.h
+++ b/mdoc.h
@@ -1,4 +1,4 @@
-/* $Id: mdoc.h,v 1.72 2009/10/26 04:09:45 kristaps Exp $ */
+/* $Id: mdoc.h,v 1.73 2009/10/30 05:58:38 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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 2505acb9..71fa9408 100644
--- a/mdoc_argv.c
+++ b/mdoc_argv.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc_argv.c,v 1.31 2009/10/28 19:21:59 kristaps Exp $ */
+/* $Id: mdoc_argv.c,v 1.32 2009/10/30 05:58:38 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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