diff options
author | Ingo Schwarze <schwarze@openbsd.org> | 2015-10-11 21:12:54 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@openbsd.org> | 2015-10-11 21:12:54 +0000 |
commit | 1efc62347acbd0d7c4f4e3e264fc1678f793560f (patch) | |
tree | f768bf7b681587553ac984a5599df4e4506f4f05 | |
parent | 4888f53396a76dbdeb8fa67b352672a0b231112f (diff) | |
download | mandoc-1efc62347acbd0d7c4f4e3e264fc1678f793560f.tar.gz |
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-- | LICENSE | 2 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | Makefile.depend | 3 | ||||
-rw-r--r-- | compat_err.c | 103 | ||||
-rwxr-xr-x | configure | 13 | ||||
-rw-r--r-- | main.c | 81 | ||||
-rw-r--r-- | mandoc_aux.c | 41 | ||||
-rw-r--r-- | mandocdb.c | 25 | ||||
-rw-r--r-- | manpath.c | 13 | ||||
-rw-r--r-- | mansearch.c | 33 | ||||
-rw-r--r-- | read.c | 20 | ||||
-rw-r--r-- | term_ps.c | 3 | ||||
-rw-r--r-- | test-err.c | 28 | ||||
-rw-r--r-- | test-progname.c | 9 |
14 files changed, 250 insertions, 130 deletions
@@ -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 @@ -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..900769c2 --- /dev/null +++ b/compat_err.c @@ -0,0 +1,103 @@ +#include "config.h" + +#if HAVE_ERR + +int dummy; + +#else + +/* $Id$ */ +/* + * 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 @@ -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 *);" @@ -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 51dd8dff..30a3bd52 100644 --- a/mandoc_aux.c +++ b/mandoc_aux.c @@ -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; } @@ -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; } @@ -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 cb7c6609..1f56de95 100644 --- a/mansearch.c +++ b/mansearch.c @@ -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; } @@ -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; } @@ -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..8ae74c0b --- /dev/null +++ b/test-err.c @@ -0,0 +1,28 @@ +/* $Id$ */ +/* + * 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"); +} |