aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2015-10-11 21:12:54 +0000
committerIngo Schwarze <schwarze@openbsd.org>2015-10-11 21:12:54 +0000
commitf9e640f1c0dc5d4fbffde207f2a4b0350d1a0019 (patch)
treeab150c9d46fcd3c701e8245e2f3d5ccbab0db7be
parentede48977046b65029014a425213d622d938c74c4 (diff)
downloadmandoc-f9e640f1c0dc5d4fbffde207f2a4b0350d1a0019.tar.gz
mandoc-f9e640f1c0dc5d4fbffde207f2a4b0350d1a0019.tar.zst
mandoc-f9e640f1c0dc5d4fbffde207f2a4b0350d1a0019.zip
Finally use __progname, err(3) and warn(3).
That's more readable and less error-prone than fumbling around with argv[0], fprintf(3), strerror(3), perror(3), and exit(3). It's a bad idea to boycott good interfaces merely because standards committees ignore them. Instead, let's provide compatibility modules for archaic systems (like commercial Solaris) that still don't have them. The compat module has an UCB Copyright (c) 1993...
-rw-r--r--LICENSE4
-rw-r--r--Makefile8
-rw-r--r--Makefile.depend3
-rw-r--r--compat_err.c103
-rwxr-xr-xconfigure13
-rw-r--r--main.c83
-rw-r--r--mandoc_aux.c43
-rw-r--r--mandocdb.c27
-rw-r--r--manpath.c15
-rw-r--r--mansearch.c35
-rw-r--r--read.c22
-rw-r--r--term_ps.c5
-rw-r--r--test-err.c28
-rw-r--r--test-progname.c9
14 files changed, 259 insertions, 139 deletions
diff --git a/LICENSE b/LICENSE
index ab32accc..15101e18 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-$Id: LICENSE,v 1.9 2015/05/20 23:43:03 schwarze Exp $
+$Id: LICENSE,v 1.10 2015/10/11 21:12:54 schwarze Exp $
With the exceptions noted below, all code and documentation
contained in the mdocml toolkit is protected by the Copyright
@@ -43,7 +43,7 @@ and 3-clause BSD licenses; see these individual files for details.
soelim.c, soelim.1:
Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
-compat_fts.c, compat_fts.h,
+compat_err.c, compat_fts.c, compat_fts.h,
compat_getsubopt.c, compat_strcasestr.c, compat_strsep.c,
man.1:
Copyright (c) 1989,1990,1993,1994 The Regents of the University of California
diff --git a/Makefile b/Makefile
index 2a7aa5aa..d26058da 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.466 2015/07/19 06:05:16 schwarze Exp $
+# $Id: Makefile,v 1.467 2015/10/11 21:12:54 schwarze Exp $
#
# Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
# Copyright (c) 2011, 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -20,6 +20,7 @@ VERSION = 1.13.3
# === LIST OF FILES ====================================================
TESTSRCS = test-dirent-namlen.c \
+ test-err.c \
test-fgetln.c \
test-fts.c \
test-getsubopt.c \
@@ -27,6 +28,7 @@ TESTSRCS = test-dirent-namlen.c \
test-mkdtemp.c \
test-mmap.c \
test-ohash.c \
+ test-progname.c \
test-reallocarray.c \
test-sqlite3.c \
test-sqlite3_errstr.c \
@@ -43,6 +45,7 @@ TESTSRCS = test-dirent-namlen.c \
SRCS = att.c \
cgi.c \
chars.c \
+ compat_err.c \
compat_fgetln.c \
compat_fts.c \
compat_getsubopt.c \
@@ -199,7 +202,8 @@ LIBMANDOC_OBJS = $(LIBMAN_OBJS) \
preconv.o \
read.o
-COMPAT_OBJS = compat_fgetln.o \
+COMPAT_OBJS = compat_err.o \
+ compat_fgetln.o \
compat_fts.o \
compat_getsubopt.o \
compat_isblank.o \
diff --git a/Makefile.depend b/Makefile.depend
index 1481d1cd..914d3908 100644
--- a/Makefile.depend
+++ b/Makefile.depend
@@ -1,6 +1,7 @@
att.o: att.c config.h roff.h mdoc.h libmdoc.h
cgi.o: cgi.c config.h mandoc_aux.h mandoc.h roff.h main.h manconf.h mansearch.h cgi.h
chars.o: chars.c config.h mandoc.h mandoc_aux.h libmandoc.h chars.in
+compat_err.o: compat_err.c config.h
compat_fgetln.o: compat_fgetln.c config.h
compat_fts.o: compat_fts.c config.h compat_fts.h
compat_getsubopt.o: compat_getsubopt.c config.h
@@ -51,7 +52,7 @@ read.o: read.c config.h mandoc_aux.h mandoc.h roff.h mdoc.h man.h libmandoc.h ro
roff.o: roff.c config.h mandoc.h mandoc_aux.h roff.h libmandoc.h roff_int.h libroff.h predefs.in
soelim.o: soelim.c config.h compat_stringlist.h
st.o: st.c config.h roff.h mdoc.h libmdoc.h st.in
-tag.o: tag.c compat_ohash.h mandoc_aux.h tag.h
+tag.o: tag.c config.h compat_ohash.h mandoc_aux.h tag.h
tbl.o: tbl.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h
tbl_data.o: tbl_data.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h
tbl_html.o: tbl_html.c config.h mandoc.h out.h html.h
diff --git a/compat_err.c b/compat_err.c
new file mode 100644
index 00000000..5107c460
--- /dev/null
+++ b/compat_err.c
@@ -0,0 +1,103 @@
+#include "config.h"
+
+#if HAVE_ERR
+
+int dummy;
+
+#else
+
+/* $Id: compat_err.c,v 1.1 2015/10/11 21:12:54 schwarze Exp $ */
+/*
+ * Copyright (c) 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+extern char *__progname;
+
+static void vwarni(const char *, va_list);
+static void vwarnxi(const char *, va_list);
+
+static void
+vwarnxi(const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: ", __progname);
+ if (fmt != NULL)
+ vfprintf(stderr, fmt, ap);
+}
+
+static void
+vwarni(const char *fmt, va_list ap)
+{
+ int sverrno;
+
+ sverrno = errno;
+ vwarnxi(fmt, ap);
+ if (fmt != NULL)
+ fputs(": ", stderr);
+ fprintf(stderr, "%s\n", strerror(sverrno));
+}
+
+void
+err(int eval, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vwarni(fmt, ap);
+ va_end(ap);
+ exit(eval);
+}
+
+void
+warn(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vwarni(fmt, ap);
+ va_end(ap);
+}
+
+void
+warnx(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vwarnxi(fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
+#endif
diff --git a/configure b/configure
index 7a3bb306..7bafcdde 100755
--- a/configure
+++ b/configure
@@ -43,12 +43,14 @@ BUILD_DB=1
BUILD_CGI=0
HAVE_DIRENT_NAMLEN=
+HAVE_ERR=
HAVE_FGETLN=
HAVE_FTS=
HAVE_GETSUBOPT=
HAVE_ISBLANK=
HAVE_MKDTEMP=
HAVE_MMAP=
+HAVE_PROGNAME=
HAVE_REALLOCARRAY=
HAVE_STRCASESTR=
HAVE_STRINGLIST=
@@ -171,12 +173,14 @@ runtest() {
# --- library functions ---
runtest dirent-namlen DIRENT_NAMLEN || true
+runtest err ERR || true
runtest fgetln FGETLN || true
runtest fts FTS || true
runtest getsubopt GETSUBOPT || true
runtest isblank ISBLANK || true
runtest mkdtemp MKDTEMP || true
runtest mmap MMAP || true
+runtest progname PROGNAME || true
runtest reallocarray REALLOCARRAY || true
runtest strcasestr STRCASESTR || true
runtest stringlist STRINGLIST || true
@@ -286,16 +290,19 @@ echo
echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\""
echo "#define MANPATH_DEFAULT \"${MANPATH_DEFAULT}\""
[ -n "${OSNAME}" ] && echo "#define OSNAME \"${OSNAME}\""
+[ ${HAVE_PROGNAME} -eq 0 ] && echo "#define __progname mandoc_progname"
[ -n "${HOMEBREWDIR}" ] && echo "#define HOMEBREWDIR \"${HOMEBREWDIR}\""
cat << __HEREDOC__
#define HAVE_DIRENT_NAMLEN ${HAVE_DIRENT_NAMLEN}
+#define HAVE_ERR ${HAVE_ERR}
#define HAVE_FGETLN ${HAVE_FGETLN}
#define HAVE_FTS ${HAVE_FTS}
#define HAVE_GETSUBOPT ${HAVE_GETSUBOPT}
#define HAVE_ISBLANK ${HAVE_ISBLANK}
#define HAVE_MKDTEMP ${HAVE_MKDTEMP}
#define HAVE_MMAP ${HAVE_MMAP}
+#define HAVE_PROGNAME ${HAVE_PROGNAME}
#define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
#define HAVE_STRCASESTR ${HAVE_STRCASESTR}
#define HAVE_STRINGLIST ${HAVE_STRINGLIST}
@@ -334,6 +341,12 @@ cat << __HEREDOC__
__HEREDOC__
+if [ ${HAVE_ERR} -eq 0 ]; then
+ echo "extern void err(int, const char *, ...);"
+ echo "extern void warn(const char *, ...);"
+ echo "extern void warnx(const char *, ...);"
+fi
+
[ ${HAVE_FGETLN} -eq 0 ] && \
echo "extern char *fgetln(FILE *, size_t *);"
diff --git a/main.c b/main.c
index 574fc64a..9ac26ce9 100644
--- a/main.c
+++ b/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.246 2015/10/10 13:21:18 schwarze Exp $ */
+/* $Id: main.c,v 1.247 2015/10/11 21:12:54 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -24,7 +24,7 @@
#include <assert.h>
#include <ctype.h>
-#include <errno.h>
+#include <err.h>
#include <fcntl.h>
#include <glob.h>
#include <signal.h>
@@ -109,10 +109,11 @@ static int toptions(struct curparse *, char *);
static void usage(enum argmode) __attribute__((noreturn));
static int woptions(struct curparse *, char *);
+extern char *__progname;
+
static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
static char help_arg[] = "help";
static char *help_argv[] = {help_arg, NULL};
-static const char *progname;
static enum mandoclevel rc;
@@ -139,15 +140,17 @@ main(int argc, char *argv[])
int use_pager;
int c;
+#if !HAVE_PROGNAME
if (argc < 1)
- progname = "mandoc";
- else if ((progname = strrchr(argv[0], '/')) == NULL)
- progname = argv[0];
+ __progname = mandoc_strdup("mandoc");
+ else if ((__progname = strrchr(argv[0], '/')) == NULL)
+ __progname = argv[0];
else
- ++progname;
+ ++__progname;
+#endif
#if HAVE_SQLITE3
- if (strcmp(progname, BINM_MAKEWHATIS) == 0)
+ if (strcmp(__progname, BINM_MAKEWHATIS) == 0)
return mandocdb(argc, argv);
#endif
@@ -160,13 +163,13 @@ main(int argc, char *argv[])
memset(&search, 0, sizeof(struct mansearch));
search.outkey = "Nd";
- if (strcmp(progname, BINM_MAN) == 0)
+ if (strcmp(__progname, BINM_MAN) == 0)
search.argmode = ARG_NAME;
- else if (strcmp(progname, BINM_APROPOS) == 0)
+ else if (strcmp(__progname, BINM_APROPOS) == 0)
search.argmode = ARG_EXPR;
- else if (strcmp(progname, BINM_WHATIS) == 0)
+ else if (strcmp(__progname, BINM_WHATIS) == 0)
search.argmode = ARG_WORD;
- else if (strncmp(progname, "help", 4) == 0)
+ else if (strncmp(__progname, "help", 4) == 0)
search.argmode = ARG_NAME;
else
search.argmode = ARG_FILE;
@@ -207,15 +210,11 @@ main(int argc, char *argv[])
break;
case 'I':
if (strncmp(optarg, "os=", 3)) {
- fprintf(stderr,
- "%s: -I %s: Bad argument\n",
- progname, optarg);
+ warnx("-I %s: Bad argument", optarg);
return (int)MANDOCLEVEL_BADARG;
}
if (defos) {
- fprintf(stderr,
- "%s: -I %s: Duplicate argument\n",
- progname, optarg);
+ warnx("-I %s: Duplicate argument", optarg);
return (int)MANDOCLEVEL_BADARG;
}
defos = mandoc_strdup(optarg + 3);
@@ -308,7 +307,7 @@ main(int argc, char *argv[])
*/
if (search.argmode == ARG_NAME) {
- if (*progname == 'h') {
+ if (*__progname == 'h') {
if (argc == 0) {
argv = help_argv;
argc = 1;
@@ -364,9 +363,7 @@ main(int argc, char *argv[])
fs_search(&search, &conf.manpath,
argc, argv, &res, &sz);
else
- fprintf(stderr,
- "%s: nothing appropriate\n",
- progname);
+ warnx("nothing appropriate");
}
if (sz == 0) {
@@ -577,8 +574,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath,
paths->paths[ipath], sec, name);
globres = glob(file, 0, NULL, &globinfo);
if (globres != 0 && globres != GLOB_NOMATCH)
- fprintf(stderr, "%s: %s: glob: %s\n",
- progname, file, strerror(errno));
+ warn("%s: glob", file);
free(file);
if (globres == 0)
file = mandoc_strdup(*globinfo.gl_pathv);
@@ -588,8 +584,8 @@ fs_lookup(const struct manpaths *paths, size_t ipath,
found:
#if HAVE_SQLITE3
- fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
- "makewhatis %s\n", progname, name, sec, paths->paths[ipath]);
+ warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s\n",
+ name, sec, paths->paths[ipath]);
#endif
*res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
page = *res + (*ressz - 1);
@@ -631,9 +627,7 @@ fs_search(const struct mansearch *cfg, const struct manpaths *paths,
return;
}
if (*ressz == lastsz)
- fprintf(stderr,
- "%s: No entry for %s in the manual.\n",
- progname, *argv);
+ warnx("No entry for %s in the manual.", *argv);
lastsz = *ressz;
argv++;
argc--;
@@ -804,8 +798,7 @@ done:
return;
fail:
- fprintf(stderr, "%s: %s: SYSERR: %s: %s",
- progname, file, syscall, strerror(errno));
+ warn("%s: SYSERR: %s", file, syscall);
if (rc < MANDOCLEVEL_SYSERR)
rc = MANDOCLEVEL_SYSERR;
}
@@ -823,8 +816,7 @@ koptions(int *options, char *arg)
} else if ( ! strcmp(arg, "us-ascii")) {
*options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
} else {
- fprintf(stderr, "%s: -K %s: Bad argument\n",
- progname, arg);
+ warnx("-K %s: Bad argument", arg);
return 0;
}
return 1;
@@ -843,8 +835,7 @@ moptions(int *options, char *arg)
else if (0 == strcmp(arg, "an"))
*options |= MPARSE_MAN;
else {
- fprintf(stderr, "%s: -m %s: Bad argument\n",
- progname, arg);
+ warnx("-m %s: Bad argument", arg);
return 0;
}
@@ -877,8 +868,7 @@ toptions(struct curparse *curp, char *arg)
else if (0 == strcmp(arg, "pdf"))
curp->outtype = OUTT_PDF;
else {
- fprintf(stderr, "%s: -T %s: Bad argument\n",
- progname, arg);
+ warnx("-T %s: Bad argument", arg);
return 0;
}
@@ -920,8 +910,7 @@ woptions(struct curparse *curp, char *arg)
curp->wlevel = MANDOCLEVEL_BADARG;
break;
default:
- fprintf(stderr, "%s: -W %s: Bad argument\n",
- progname, o);
+ warnx("-W %s: Bad argument", o);
return 0;
}
}
@@ -935,7 +924,7 @@ mmsg(enum mandocerr t, enum mandoclevel lvl,
{
const char *mparse_msg;
- fprintf(stderr, "%s: %s:", progname, file);
+ fprintf(stderr, "%s: %s:", __progname, file);
if (line)
fprintf(stderr, "%d:%d:", line, col + 1);
@@ -1001,9 +990,7 @@ spawn_pager(struct tag_files *tag_files)
switch (pager_pid = fork()) {
case -1:
- fprintf(stderr, "%s: fork: %s\n",
- progname, strerror(errno));
- exit((int)MANDOCLEVEL_SYSERR);
+ err((int)MANDOCLEVEL_SYSERR, "fork");
case 0:
break;
default:
@@ -1012,14 +999,10 @@ spawn_pager(struct tag_files *tag_files)
/* The child process becomes the pager. */
- if (dup2(tag_files->ofd, STDOUT_FILENO) == -1) {
- fprintf(stderr, "pager: stdout: %s\n", strerror(errno));
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
+ err((int)MANDOCLEVEL_SYSERR, "pager stdout");
close(tag_files->ofd);
close(tag_files->tfd);
execvp(argv[0], argv);
- fprintf(stderr, "%s: exec %s: %s\n",
- progname, argv[0], strerror(errno));
- exit((int)MANDOCLEVEL_SYSERR);
+ err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
}
diff --git a/mandoc_aux.c b/mandoc_aux.c
index 622c4642..28a6e2f7 100644
--- a/mandoc_aux.c
+++ b/mandoc_aux.c
@@ -1,4 +1,4 @@
-/* $Id: mandoc_aux.c,v 1.5 2015/10/06 18:32:19 schwarze Exp $ */
+/* $Id: mandoc_aux.c,v 1.6 2015/10/11 21:12:54 schwarze Exp $ */
/*
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -19,6 +19,7 @@
#include <sys/types.h>
+#include <err.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
@@ -27,6 +28,10 @@
#include "mandoc.h"
#include "mandoc_aux.h"
+#if !HAVE_PROGNAME
+const char *mandoc_progname;
+#endif
+
int
mandoc_asprintf(char **dest, const char *fmt, ...)
{
@@ -37,10 +42,8 @@ mandoc_asprintf(char **dest, const char *fmt, ...)
ret = vasprintf(dest, fmt, ap);
va_end(ap);
- if (-1 == ret) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ret == -1)
+ err((int)MANDOCLEVEL_SYSERR, NULL);
return ret;
}
@@ -50,10 +53,8 @@ mandoc_calloc(size_t num, size_t size)
void *ptr;
ptr = calloc(num, size);
- if (NULL == ptr) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ptr == NULL)
+ err((int)MANDOCLEVEL_SYSERR, NULL);
return ptr;
}
@@ -63,10 +64,8 @@ mandoc_malloc(size_t size)
void *ptr;
ptr = malloc(size);
- if (NULL == ptr) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ptr == NULL)
+ err((int)MANDOCLEVEL_SYSERR, NULL);
return ptr;
}
@@ -75,10 +74,8 @@ mandoc_realloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
- if (NULL == ptr) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ptr == NULL)
+ err((int)MANDOCLEVEL_SYSERR, NULL);
return ptr;
}
@@ -87,10 +84,8 @@ mandoc_reallocarray(void *ptr, size_t num, size_t size)
{
ptr = reallocarray(ptr, num, size);
- if (NULL == ptr) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ptr == NULL)
+ err((int)MANDOCLEVEL_SYSERR, NULL);
return ptr;
}
@@ -100,10 +95,8 @@ mandoc_strdup(const char *ptr)
char *p;
p = strdup(ptr);
- if (NULL == p) {
- perror(NULL);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ptr == NULL)
+ err((int)MANDOCLEVEL_SYSERR, NULL);
return p;
}
diff --git a/mandocdb.c b/mandocdb.c
index e09b8029..fda1ec61 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -1,4 +1,4 @@
-/* $Id: mandocdb.c,v 1.197 2015/10/06 18:32:19 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.198 2015/10/11 21:12:54 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -23,6 +23,7 @@
#include <assert.h>
#include <ctype.h>
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#if HAVE_FTS
@@ -189,8 +190,9 @@ static int set_basedir(const char *, int);
static int treescan(void);
static size_t utf8(unsigned int, char [7]);
+extern char *__progname;
+
static char tempfilename[32];
-static char *progname;
static int nodb; /* no database changes */
static int mparse_options; /* abort the parse early */
static int use_all; /* use all found files */
@@ -357,12 +359,6 @@ mandocdb(int argc, char *argv[])
mpages_info.key_offset = offsetof(struct mpage, inodev);
mlinks_info.key_offset = offsetof(struct mlink, file);
- progname = strrchr(argv[0], '/');
- if (progname == NULL)
- progname = argv[0];
- else
- ++progname;
-
/*
* We accept a few different invocations.
* The CHECKOP macro makes sure that invocation styles don't
@@ -370,8 +366,7 @@ mandocdb(int argc, char *argv[])
*/
#define CHECKOP(_op, _ch) do \
if (OP_DEFAULT != (_op)) { \
- fprintf(stderr, "%s: -%c: Conflicting option\n", \
- progname, (_ch)); \
+ warnx("-%c: Conflicting option", (_ch)); \
goto usage; \
} while (/*CONSTCOND*/0)
@@ -407,9 +402,8 @@ mandocdb(int argc, char *argv[])
break;
case 'T':
if (strcmp(optarg, "utf8")) {
- fprintf(stderr, "%s: -T%s: "
- "Unsupported output format\n",
- progname, optarg);
+ warnx("-T%s: Unsupported output format",
+ optarg);
goto usage;
}
write_utf8 = 1;
@@ -436,8 +430,7 @@ mandocdb(int argc, char *argv[])
argv += optind;
if (OP_CONFFILE == op && argc > 0) {
- fprintf(stderr, "%s: -C: Too many arguments\n",
- progname);
+ warnx("-C: Too many arguments");
goto usage;
}
@@ -554,8 +547,8 @@ usage:
" %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
" %s [-Dnp] -u dir [file ...]\n"
" %s [-Q] -t file ...\n",
- progname, progname, progname,
- progname, progname);
+ __progname, __progname, __progname,
+ __progname, __progname);
return (int)MANDOCLEVEL_BADARG;
}
diff --git a/manpath.c b/manpath.c
index 532bdb30..f912e766 100644
--- a/manpath.c
+++ b/manpath.c
@@ -1,4 +1,4 @@
-/* $Id: manpath.c,v 1.26 2015/06/10 19:26:13 schwarze Exp $ */
+/* $Id: manpath.c,v 1.27 2015/10/11 21:12:55 schwarze Exp $ */
/*
* Copyright (c) 2011, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
@@ -21,6 +21,7 @@
#include <sys/stat.h>
#include <ctype.h>
+#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -165,10 +166,8 @@ manpath_add(struct manpaths *dirs, const char *dir, int complain)
size_t i;
if (NULL == (cp = realpath(dir, buf))) {
- if (complain) {
- fputs("manpath: ", stderr);
- perror(dir);
- }
+ if (complain)
+ warn("manpath: %s", dir);
return;
}
@@ -177,10 +176,8 @@ manpath_add(struct manpaths *dirs, const char *dir, int complain)
return;
if (stat(cp, &sb) == -1) {
- if (complain) {
- fputs("manpath: ", stderr);
- perror(dir);
- }
+ if (complain)
+ warn("manpath: %s", dir);
return;
}
diff --git a/mansearch.c b/mansearch.c
index e019b487..def52984 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.58 2015/10/06 18:32:19 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.59 2015/10/11 21:12:55 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <assert.h>
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
@@ -55,17 +56,17 @@ extern const char *const mansearch_keynames[];
#define SQL_BIND_TEXT(_db, _s, _i, _v) \
do { if (SQLITE_OK != sqlite3_bind_text \
((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
- fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
+ warnx("%s", sqlite3_errmsg((_db))); \
} while (0)
#define SQL_BIND_INT64(_db, _s, _i, _v) \
do { if (SQLITE_OK != sqlite3_bind_int64 \
((_s), (_i)++, (_v))) \
- fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
+ warnx("%s", sqlite3_errmsg((_db))); \
} while (0)
#define SQL_BIND_BLOB(_db, _s, _i, _v) \
do { if (SQLITE_OK != sqlite3_bind_blob \
((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
- fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
+ warnx("%s", sqlite3_errmsg((_db))); \
} while (0)
struct expr {
@@ -120,7 +121,7 @@ mansearch_setup(int start)
if (start) {
if (NULL != pagecache) {
- fprintf(stderr, "pagecache already enabled\n");
+ warnx("pagecache already enabled");
return (int)MANDOCLEVEL_BADARG;
}
@@ -140,10 +141,10 @@ mansearch_setup(int start)
if (SQLITE_OK == c)
return (int)MANDOCLEVEL_OK;
- fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
+ warnx("pagecache: %s", sqlite3_errstr(c));
} else if (NULL == pagecache) {
- fprintf(stderr, "pagecache missing\n");
+ warnx("pagecache missing");
return (int)MANDOCLEVEL_BADARG;
}
@@ -233,8 +234,7 @@ mansearch(const struct mansearch *search,
for (i = 0; i < paths->sz; i++) {
if (chdir_status && paths->paths[i][0] != '/') {
if ( ! getcwd_status) {
- fprintf(stderr, "%s: getcwd: %s\n",
- paths->paths[i], buf);
+ warnx("%s: getcwd: %s", paths->paths[i], buf);
continue;
} else if (chdir(buf) == -1) {
perror(buf);
@@ -251,8 +251,7 @@ mansearch(const struct mansearch *search,
SQLITE_OPEN_READONLY, NULL);
if (SQLITE_OK != c) {
- fprintf(stderr, "%s/%s: %s\n",
- paths->paths[i], MANDOC_DB, strerror(errno));
+ warn("%s/%s", paths->paths[i], MANDOC_DB);
sqlite3_close(db);
continue;
}
@@ -274,7 +273,7 @@ mansearch(const struct mansearch *search,
j = 1;
c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
if (SQLITE_OK != c)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ warnx("%s", sqlite3_errmsg(db));
for (ep = e; NULL != ep; ep = ep->next) {
if (NULL == ep->substr) {
@@ -316,7 +315,7 @@ mansearch(const struct mansearch *search,
}
if (SQLITE_DONE != c)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(s);
@@ -325,14 +324,14 @@ mansearch(const struct mansearch *search,
"WHERE pageid=? ORDER BY sec, arch, name",
-1, &s, NULL);
if (SQLITE_OK != c)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ warnx("%s", sqlite3_errmsg(db));
c = sqlite3_prepare_v2(db,
"SELECT bits, key, pageid FROM keys "
"WHERE pageid=? AND bits & ?",
-1, &s2, NULL);
if (SQLITE_OK != c)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ warnx("%s", sqlite3_errmsg(db));
for (mp = ohash_first(&htab, &idx);
NULL != mp;
@@ -513,7 +512,7 @@ buildnames(const struct mansearch *search, struct manpage *mpage,
globfree(&globinfo);
}
if (c != SQLITE_DONE)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ warnx("%s", sqlite3_errmsg(db));
sqlite3_reset(s);
/* If none of the files is usable, use the first name. */
@@ -563,7 +562,7 @@ buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
output = newoutput;
}
if (SQLITE_DONE != c)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ warnx("%s", sqlite3_errmsg(db));
sqlite3_reset(s);
return output;
}
@@ -805,7 +804,7 @@ exprterm(const struct mansearch *search, char *buf, int cs)
free(val);
if (irc) {
regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
- fprintf(stderr, "regcomp: %s\n", errbuf);
+ warnx("regcomp: %s", errbuf);
free(e);
return NULL;
}
diff --git a/read.c b/read.c
index e15a813d..9af8ad4a 100644
--- a/read.c
+++ b/read.c
@@ -1,4 +1,4 @@
-/* $Id: read.c,v 1.142 2015/10/06 18:32:19 schwarze Exp $ */
+/* $Id: read.c,v 1.143 2015/10/11 21:12:55 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -26,6 +26,7 @@
#include <assert.h>
#include <ctype.h>
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
@@ -612,10 +613,9 @@ read_whole_file(struct mparse *curp, const char *file, int fd,
#if HAVE_MMAP
struct stat st;
- if (-1 == fstat(fd, &st)) {
- perror(file);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+
+ if (fstat(fd, &st) == -1)
+ err((int)MANDOCLEVEL_SYSERR, "%s", file);
/*
* If we're a regular file, try just reading in the whole entry
@@ -638,10 +638,8 @@ read_whole_file(struct mparse *curp, const char *file, int fd,
#endif
if (curp->gzip) {
- if ((gz = gzdopen(fd, "rb")) == NULL) {
- perror(file);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if ((gz = gzdopen(fd, "rb")) == NULL)
+ err((int)MANDOCLEVEL_SYSERR, "%s", file);
} else
gz = NULL;
@@ -670,10 +668,8 @@ read_whole_file(struct mparse *curp, const char *file, int fd,
fb->sz = off;
return 1;
}
- if (ssz == -1) {
- perror(file);
- exit((int)MANDOCLEVEL_SYSERR);
- }
+ if (ssz == -1)
+ err((int)MANDOCLEVEL_SYSERR, "%s", file);
off += (size_t)ssz;
}
diff --git a/term_ps.c b/term_ps.c
index 1d3864f9..2d546e2e 100644
--- a/term_ps.c
+++ b/term_ps.c
@@ -1,4 +1,4 @@
-/* $Id: term_ps.c,v 1.75 2015/10/06 18:32:20 schwarze Exp $ */
+/* $Id: term_ps.c,v 1.76 2015/10/11 21:12:55 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <assert.h>
+#include <err.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
@@ -582,7 +583,7 @@ pspdf_alloc(const struct mchars *mchars, const struct manoutput *outopts)
pagex = 216;
pagey = 356;
} else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey))
- fprintf(stderr, "%s: Unknown paper\n", pp);
+ warnx("%s: Unknown paper", pp);
}
/*
diff --git a/test-err.c b/test-err.c
new file mode 100644
index 00000000..d17e0f0d
--- /dev/null
+++ b/test-err.c
@@ -0,0 +1,28 @@
+/* $Id: test-err.c,v 1.1 2015/10/11 21:12:55 schwarze Exp $ */
+/*
+ * Copyright (c) 2015 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
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <err.h>
+
+int
+main(void)
+{
+ warnx("%d. warnx", 1);
+ warn("%d. warn", 2);
+ err(0, "%d. err", 3);
+ /* NOTREACHED */
+ return 1;
+}
diff --git a/test-progname.c b/test-progname.c
new file mode 100644
index 00000000..79add7eb
--- /dev/null
+++ b/test-progname.c
@@ -0,0 +1,9 @@
+#include <string.h>
+
+extern char *__progname;
+
+int
+main(void)
+{
+ return !!strcmp(__progname, "test-progname");
+}