summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2014-03-21 22:52:22 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2014-03-21 22:52:22 +0000
commit8b94d80adb28708667f9499981981bc733a9bdbf (patch)
tree64200a6ea4ab02ea932bf4b03c378057b0443193 /usr.bin
parent8c2e8278819af3eeaaa829b00554902f74fb874c (diff)
avoid repetitive code for asprintf error handling
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mandoc/mandoc_aux.c21
-rw-r--r--usr.bin/mandoc/mandoc_aux.h4
-rw-r--r--usr.bin/mandoc/mandocdb.c7
-rw-r--r--usr.bin/mandoc/mansearch.c42
-rw-r--r--usr.bin/mandoc/mdoc.c7
-rw-r--r--usr.bin/mandoc/mdoc_validate.c10
-rw-r--r--usr.bin/mandoc/roff.c8
7 files changed, 45 insertions, 54 deletions
diff --git a/usr.bin/mandoc/mandoc_aux.c b/usr.bin/mandoc/mandoc_aux.c
index 439b7958223..f638b301661 100644
--- a/usr.bin/mandoc/mandoc_aux.c
+++ b/usr.bin/mandoc/mandoc_aux.c
@@ -1,6 +1,7 @@
-/* $Id: mandoc_aux.c,v 1.1 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: mandoc_aux.c,v 1.2 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -16,6 +17,7 @@
*/
#include <sys/types.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -23,6 +25,23 @@
#include "mandoc.h"
#include "mandoc_aux.h"
+int
+mandoc_asprintf(char **dest, const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, fmt);
+ ret = vasprintf(dest, fmt, ap);
+ va_end(ap);
+
+ if (-1 == ret) {
+ perror(NULL);
+ exit((int)MANDOCLEVEL_SYSERR);
+ }
+ return(ret);
+}
+
void *
mandoc_calloc(size_t num, size_t size)
{
diff --git a/usr.bin/mandoc/mandoc_aux.h b/usr.bin/mandoc/mandoc_aux.h
index 1d2077eaebc..4f178c789c1 100644
--- a/usr.bin/mandoc/mandoc_aux.h
+++ b/usr.bin/mandoc/mandoc_aux.h
@@ -1,6 +1,7 @@
-/* $Id: mandoc_aux.h,v 1.1 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: mandoc_aux.h,v 1.2 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -19,6 +20,7 @@
__BEGIN_DECLS
+int mandoc_asprintf(char **, const char *, ...);
void *mandoc_calloc(size_t, size_t);
void *mandoc_malloc(size_t);
void *mandoc_realloc(void *, size_t);
diff --git a/usr.bin/mandoc/mandocdb.c b/usr.bin/mandoc/mandocdb.c
index 3546cfa1f53..cca30fb134b 100644
--- a/usr.bin/mandoc/mandocdb.c
+++ b/usr.bin/mandoc/mandocdb.c
@@ -1,4 +1,4 @@
-/* $Id: mandocdb.c,v 1.76 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.77 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -1528,10 +1528,7 @@ parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
return(0);
}
- if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string);
putkey(mpage, cp, TYPE_Xr);
free(cp);
return(0);
diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c
index c7ac15b85b8..1f0114ef06a 100644
--- a/usr.bin/mandoc/mansearch.c
+++ b/usr.bin/mandoc/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.14 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.15 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -330,11 +330,8 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
(strcmp(sec, prevsec) ||
strcmp(arch, prevarch))) {
sep2 = '\0' == *prevarch ? "" : "/";
- if (-1 == asprintf(&newnames, "%s(%s%s%s)",
- oldnames, prevsec, sep2, prevarch)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&newnames, "%s(%s%s%s)",
+ oldnames, prevsec, sep2, prevarch);
free(mpage->names);
oldnames = mpage->names = newnames;
free(prevsec);
@@ -351,11 +348,8 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
/* Append the new name. */
- if (-1 == asprintf(&newnames, "%s%s%s",
- oldnames, sep1, name)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&newnames, "%s%s%s",
+ oldnames, sep1, name);
free(mpage->names);
mpage->names = newnames;
@@ -372,11 +366,8 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
fsec = "0";
}
sep2 = '\0' == *arch ? "" : "/";
- if (-1 == asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
- path, sep1, sec, sep2, arch, name, fsec)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
+ path, sep1, sec, sep2, arch, name, fsec);
}
if (SQLITE_DONE != c)
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
@@ -386,11 +377,8 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
if (NULL != prevsec) {
sep2 = '\0' == *prevarch ? "" : "/";
- if (-1 == asprintf(&newnames, "%s(%s%s%s)",
- mpage->names, prevsec, sep2, prevarch)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&newnames, "%s(%s%s%s)",
+ mpage->names, prevsec, sep2, prevarch);
free(mpage->names);
mpage->names = newnames;
free(prevsec);
@@ -419,11 +407,8 @@ buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, uint64_t outbit)
sep1 = " # ";
}
data = sqlite3_column_text(s, 1);
- if (-1 == asprintf(&newoutput, "%s%s%s",
- oldoutput, sep1, data)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&newoutput, "%s%s%s",
+ oldoutput, sep1, data);
free(output);
output = newoutput;
}
@@ -595,10 +580,7 @@ exprspec(struct expr *cur, uint64_t key, const char *value,
if (NULL == value)
return(cur);
- if (-1 == asprintf(&cp, format, value)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&cp, format, value);
cur->next = mandoc_calloc(1, sizeof(struct expr));
cur = cur->next;
cur->and = 1;
diff --git a/usr.bin/mandoc/mdoc.c b/usr.bin/mandoc/mdoc.c
index 0b9b141db08..f13b9704500 100644
--- a/usr.bin/mandoc/mdoc.c
+++ b/usr.bin/mandoc/mdoc.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc.c,v 1.99 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: mdoc.c,v 1.100 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -590,10 +590,7 @@ mdoc_word_append(struct mdoc *mdoc, const char *p)
n = mdoc->last;
addstr = roff_strdup(mdoc->roff, p);
- if (-1 == asprintf(&newstr, "%s %s", n->string, addstr)) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
free(addstr);
free(n->string);
n->string = newstr;
diff --git a/usr.bin/mandoc/mdoc_validate.c b/usr.bin/mandoc/mdoc_validate.c
index 39b302bd16d..b1baac9cc3f 100644
--- a/usr.bin/mandoc/mdoc_validate.c
+++ b/usr.bin/mandoc/mdoc_validate.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc_validate.c,v 1.122 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: mdoc_validate.c,v 1.123 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -2393,11 +2393,9 @@ post_os(POST_ARGS)
if (-1 == uname(&utsname)) {
mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
defbuf = mandoc_strdup("UNKNOWN");
- } else if (-1 == asprintf(&defbuf, "%s %s",
- utsname.sysname, utsname.release)) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ } else
+ mandoc_asprintf(&defbuf, "%s %s",
+ utsname.sysname, utsname.release);
}
mdoc->meta.os = mandoc_strdup(defbuf);
#endif /*!OSNAME*/
diff --git a/usr.bin/mandoc/roff.c b/usr.bin/mandoc/roff.c
index caced523938..0084569fb7d 100644
--- a/usr.bin/mandoc/roff.c
+++ b/usr.bin/mandoc/roff.c
@@ -1,4 +1,4 @@
-/* $Id: roff.c,v 1.74 2014/03/21 22:17:01 schwarze Exp $ */
+/* $Id: roff.c,v 1.75 2014/03/21 22:52:21 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -652,11 +652,7 @@ roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
/* Spring the input line trap. */
if (1 == roffit_lines) {
- isz = asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
- if (-1 == isz) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ isz = mandoc_asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
free(*bufp);
*bufp = p;
*szp = isz + 1;